Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: a09c94ae62e9d9ed92f05e3a720604aa210e8883 [file] [log] [blame]
drh75897232000-05-29 14:26:001/*
drhb19a2bc2001-09-16 00:13:262** 2001 September 15
drh75897232000-05-29 14:26:003**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:006**
drhb19a2bc2001-09-16 00:13:267** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:0010**
11*************************************************************************
12** Internal interface definitions for SQLite.
13**
drh75897232000-05-29 14:26:0014*/
drh43f58d62016-07-09 16:14:4515#ifndef SQLITEINT_H
16#define SQLITEINT_H
drh71674ce2007-10-23 15:51:2617
drh396794f2016-04-28 20:11:1218/* Special Comments:
19**
20** Some comments have special meaning to the tools that measure test
21** coverage:
22**
23** NO_TEST - The branches on this line are not
24** measured by branch coverage. This is
25** used on lines of code that actually
26** implement parts of coverage testing.
27**
larrybrbc917382023-06-07 08:40:3128** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
drh396794f2016-04-28 20:11:1229** and the correct answer is still obtained,
30** though perhaps more slowly.
31**
larrybrbc917382023-06-07 08:40:3132** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
drh396794f2016-04-28 20:11:1233** and the correct answer is still obtained,
34** though perhaps more slowly.
35**
36** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
37** that would be harmless and undetectable
larrybrbc917382023-06-07 08:40:3138** if it did occur.
drh396794f2016-04-28 20:11:1239**
40** In all cases, the special comment must be enclosed in the usual
larrybrbc917382023-06-07 08:40:3141** slash-asterisk...asterisk-slash comment marks, with no spaces between the
drh396794f2016-04-28 20:11:1242** asterisks and the comment text.
43*/
44
mlcreechbd0ae112008-03-06 09:16:2445/*
mistachkin7617e4a2016-07-28 17:11:2046** Make sure the Tcl calling convention macro is defined. This macro is
47** only used by test code and Tcl integration code.
48*/
49#ifndef SQLITE_TCLAPI
50# define SQLITE_TCLAPI
51#endif
drh71674ce2007-10-23 15:51:2652
mlcreechbd0ae112008-03-06 09:16:2453/*
mistachkin2318d332015-01-12 18:02:5254** Include the header file used to customize the compiler options for MSVC.
55** This should be done first so that it can successfully prevent spurious
56** compiler warnings due to subsequent content in this file and other files
57** that are included by this file.
58*/
59#include "msvc.h"
60
61/*
drh8cd5b252015-03-02 22:06:4362** Special setup for VxWorks
63*/
64#include "vxworks.h"
65
66/*
drh2210dcc2009-08-12 11:45:4067** These #defines should enable >2GB file support on POSIX if the
68** underlying operating system supports it. If the OS lacks
69** large file support, or if the OS is windows, these should be no-ops.
70**
71** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
72** system #includes. Hence, this block of code must be the very first
73** code in all source files.
74**
75** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
76** on the compiler command line. This is necessary if you are compiling
77** on a recent machine (ex: Red Hat 7.2) but you want your code to work
78** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
79** without this option, LFS is enable. But LFS does not exist in the kernel
80** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
81** portability you should omit LFS.
82**
drhdddf6972014-02-07 19:33:3183** The previous paragraph was written in 2005. (This paragraph is written
84** on 2008-11-28.) These days, all Linux kernels support large files, so
85** you should probably leave LFS enabled. But some embedded platforms might
86** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
87**
drh2210dcc2009-08-12 11:45:4088** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
89*/
90#ifndef SQLITE_DISABLE_LFS
91# define _LARGE_FILE 1
92# ifndef _FILE_OFFSET_BITS
93# define _FILE_OFFSET_BITS 64
94# endif
95# define _LARGEFILE_SOURCE 1
96#endif
97
drhdc5ece82017-02-15 15:09:0998/* The GCC_VERSION and MSVC_VERSION macros are used to
drha39284b2017-02-09 17:12:2299** conditionally include optimizations for each of these compilers. A
100** value of 0 means that compiler is not being used. The
101** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
102** optimizations, and hence set all compiler macros to 0
drhdc5ece82017-02-15 15:09:09103**
104** There was once also a CLANG_VERSION macro. However, we learn that the
105** version numbers in clang are for "marketing" only and are inconsistent
106** and unreliable. Fortunately, all versions of clang also recognize the
107** gcc version numbers and have reasonable settings for gcc version numbers,
108** so the GCC_VERSION macro will be set to a correct non-zero value even
109** when compiling with clang.
drha39284b2017-02-09 17:12:22110*/
111#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
drhad265292015-06-30 14:01:20112# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
113#else
114# define GCC_VERSION 0
115#endif
drha39284b2017-02-09 17:12:22116#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
117# define MSVC_VERSION _MSC_VER
118#else
119# define MSVC_VERSION 0
120#endif
drhad265292015-06-30 14:01:20121
mistachkind97a4c02020-12-09 23:35:51122/*
123** Some C99 functions in "math.h" are only present for MSVC when its version
124** is associated with Visual Studio 2013 or higher.
125*/
126#ifndef SQLITE_HAVE_C99_MATH_FUNCS
127# if MSVC_VERSION==0 || MSVC_VERSION>=1800
128# define SQLITE_HAVE_C99_MATH_FUNCS (1)
129# else
130# define SQLITE_HAVE_C99_MATH_FUNCS (0)
131# endif
132#endif
133
mistachkin33ac4c82014-09-20 00:02:23134/* Needed for various definitions... */
135#if defined(__GNUC__) && !defined(_GNU_SOURCE)
136# define _GNU_SOURCE
137#endif
138
139#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
140# define _BSD_SOURCE
141#endif
142
drh2210dcc2009-08-12 11:45:40143/*
drh08b92082020-08-10 14:18:00144** Macro to disable warnings about missing "break" at the end of a "case".
145*/
drh8960c022024-07-02 12:16:29146#if defined(__has_attribute)
147# if __has_attribute(fallthrough)
148# define deliberate_fall_through __attribute__((fallthrough));
149# endif
150#endif
151#if !defined(deliberate_fall_through)
152# define deliberate_fall_through
drh08b92082020-08-10 14:18:00153#endif
154
155/*
mistachkina7b3b632014-02-14 23:35:49156** For MinGW, check to see if we can include the header file containing its
157** version information, among other things. Normally, this internal MinGW
158** header file would [only] be included automatically by other MinGW header
159** files; however, the contained version information is now required by this
160** header file to work around binary compatibility issues (see below) and
161** this is the only known way to reliably obtain it. This entire #if block
162** would be completely unnecessary if there was any other way of detecting
163** MinGW via their preprocessor (e.g. if they customized their GCC to define
164** some MinGW-specific macros). When compiling for MinGW, either the
165** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
166** defined; otherwise, detection of conditions specific to MinGW will be
167** disabled.
168*/
169#if defined(_HAVE_MINGW_H)
170# include "mingw.h"
171#elif defined(_HAVE__MINGW_H)
172# include "_mingw.h"
173#endif
174
175/*
176** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
177** define is required to maintain binary compatibility with the MSVC runtime
178** library in use (e.g. for Windows XP).
179*/
180#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
181 defined(_WIN32) && !defined(_WIN64) && \
mistachkin65acf372014-02-16 19:20:00182 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
mistachkina7b3b632014-02-14 23:35:49183 defined(__MSVCRT__)
184# define _USE_32BIT_TIME_T
185#endif
186
larrybrf382e1d2021-07-08 22:12:27187/* Optionally #include a user-defined header, whereby compilation options
larrybrbc917382023-06-07 08:40:31188** may be set prior to where they take effect, but after platform setup.
mistachkinb5231592021-07-08 23:35:20189** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
190** file.
larrybrf382e1d2021-07-08 22:12:27191*/
mistachkinb5231592021-07-08 23:35:20192#ifdef SQLITE_CUSTOM_INCLUDE
larrybrf382e1d2021-07-08 22:12:27193# define INC_STRINGIFY_(f) #f
194# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
mistachkinb5231592021-07-08 23:35:20195# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
larrybrf382e1d2021-07-08 22:12:27196#endif
197
drh5e990be2014-02-25 14:52:01198/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
mistachkin0d71d122014-03-06 00:28:57199** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
200** MinGW.
drh5e990be2014-02-25 14:52:01201*/
202#include "sqlite3.h"
203
mlcreechbd0ae112008-03-06 09:16:24204/*
drh18a3a482022-09-02 00:36:16205** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
206*/
drhfee64312022-09-02 11:12:16207#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1
drh18a3a482022-09-02 00:36:16208
209/*
mlcreech1e12d432008-05-07 02:42:01210** Include the configuration header output by 'configure' if we're using the
211** autoconf-based build
mlcreechbd0ae112008-03-06 09:16:24212*/
drh7f2d1cd2017-06-24 16:35:00213#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
drha4b2f412022-10-04 10:35:10214#include "sqlite_cfg.h"
drh7f2d1cd2017-06-24 16:35:00215#define SQLITECONFIG_H 1
mlcreech1e12d432008-05-07 02:42:01216#endif
217
drhbb4957f2008-03-20 14:03:29218#include "sqliteLimit.h"
mlcreech98dc4b12008-03-06 16:28:58219
drhe2965822008-04-10 16:47:41220/* Disable nuisance warnings on Borland compilers */
221#if defined(__BORLANDC__)
222#pragma warn -rch /* unreachable code */
223#pragma warn -ccc /* Condition is always true or false */
224#pragma warn -aus /* Assigned value is never used */
225#pragma warn -csu /* Comparing signed and unsigned */
shane467bcf32008-11-24 20:01:32226#pragma warn -spa /* Suspicious pointer arithmetic */
drhe2965822008-04-10 16:47:41227#endif
228
mlcreech98dc4b12008-03-06 16:28:58229/*
drhad96db82023-02-23 14:22:29230** A few places in the code require atomic load/store of aligned
231** integer values.
dan892edb62020-03-30 13:35:05232*/
danb5538942020-06-04 16:34:49233#ifndef __has_extension
234# define __has_extension(x) 0 /* compatibility with non-clang compilers */
dan892edb62020-03-30 13:35:05235#endif
larrybrbc917382023-06-07 08:40:31236#if GCC_VERSION>=4007000 || __has_extension(c_atomic)
drha612c1c2021-07-05 18:37:37237# define SQLITE_ATOMIC_INTRINSICS 1
dan892edb62020-03-30 13:35:05238# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
239# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
240#else
drha612c1c2021-07-05 18:37:37241# define SQLITE_ATOMIC_INTRINSICS 0
dan892edb62020-03-30 13:35:05242# define AtomicLoad(PTR) (*(PTR))
243# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
244#endif
245
246/*
drh06af7632008-04-28 12:54:15247** Include standard header files as necessary
248*/
249#ifdef HAVE_STDINT_H
250#include <stdint.h>
251#endif
252#ifdef HAVE_INTTYPES_H
253#include <inttypes.h>
254#endif
255
drha2460e02010-01-14 00:39:26256/*
drhc05a9a82010-03-04 16:12:34257** The following macros are used to cast pointers to integers and
258** integers to pointers. The way you do this varies from one compiler
259** to the next, so we have developed the following set of #if statements
260** to generate appropriate macros for a wide range of compilers.
drh875e9e72009-05-16 17:38:21261**
mistachkinbfc9b3f2016-02-15 22:01:24262** The correct "ANSI" way to do this is to use the intptr_t type.
drhc05a9a82010-03-04 16:12:34263** Unfortunately, that typedef is not available on all compilers, or
264** if it is available, it requires an #include of specific headers
drhc92271c2010-03-10 14:06:35265** that vary from one machine to the next.
drh875e9e72009-05-16 17:38:21266**
267** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
268** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
drh5e49edc2009-05-18 13:34:37269** So we have to define the macros in different ways depending on the
drh875e9e72009-05-16 17:38:21270** compiler.
271*/
drh4509ffa2019-07-17 19:57:55272#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
273# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
274# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
275#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
drhc05a9a82010-03-04 16:12:34276# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
277# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
278#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
279# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
280# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
drhc05a9a82010-03-04 16:12:34281#else /* Generates a warning - but it always works */
282# define SQLITE_INT_TO_PTR(X) ((void*)(X))
283# define SQLITE_PTR_TO_INT(X) ((int)(X))
drh875e9e72009-05-16 17:38:21284#endif
drhb6dbc002007-11-27 02:38:00285
drhe5e7a902007-10-01 17:47:00286/*
drh706c33d2023-04-07 15:07:58287** Macros to hint to the compiler that a function should or should not be
drh14a924a2014-08-22 14:34:05288** inlined.
289*/
290#if defined(__GNUC__)
291# define SQLITE_NOINLINE __attribute__((noinline))
drh34ceb7e2023-04-07 14:33:33292# define SQLITE_INLINE __attribute__((always_inline)) inline
drhab993382014-10-10 18:09:52293#elif defined(_MSC_VER) && _MSC_VER>=1310
drh14a924a2014-08-22 14:34:05294# define SQLITE_NOINLINE __declspec(noinline)
drh34ceb7e2023-04-07 14:33:33295# define SQLITE_INLINE __forceinline
drh14a924a2014-08-22 14:34:05296#else
297# define SQLITE_NOINLINE
drh34ceb7e2023-04-07 14:33:33298# define SQLITE_INLINE
drh14a924a2014-08-22 14:34:05299#endif
drh0f97dc22023-05-01 20:09:52300#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__)
drh706c33d2023-04-07 15:07:58301# undef SQLITE_INLINE
302# define SQLITE_INLINE
303#endif
drh14a924a2014-08-22 14:34:05304
305/*
mistachkin647ca462015-06-30 17:28:40306** Make sure that the compiler intrinsics we desire are enabled when
mistachkin60e08072015-07-29 21:47:39307** compiling with an appropriate version of MSVC unless prevented by
308** the SQLITE_DISABLE_INTRINSIC define.
mistachkin647ca462015-06-30 17:28:40309*/
mistachkin60e08072015-07-29 21:47:39310#if !defined(SQLITE_DISABLE_INTRINSIC)
mistachkin11f69b82016-07-29 17:36:27311# if defined(_MSC_VER) && _MSC_VER>=1400
mistachkin60e08072015-07-29 21:47:39312# if !defined(_WIN32_WCE)
313# include <intrin.h>
314# pragma intrinsic(_byteswap_ushort)
315# pragma intrinsic(_byteswap_ulong)
drha39284b2017-02-09 17:12:22316# pragma intrinsic(_byteswap_uint64)
mistachkin8d9837a2015-10-06 01:44:53317# pragma intrinsic(_ReadWriteBarrier)
mistachkin60e08072015-07-29 21:47:39318# else
319# include <cmnintrin.h>
320# endif
mistachkin9895f732015-07-24 20:43:18321# endif
mistachkin647ca462015-06-30 17:28:40322#endif
323
324/*
drh50da20d2023-10-09 14:05:21325** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit
326** SEH support if the -DSQLITE_OMIT_SEH option is given.
327*/
328#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
329# define SQLITE_USE_SEH 1
330#else
drh7e60f6d2023-10-09 14:47:25331# undef SQLITE_USE_SEH
drh50da20d2023-10-09 14:05:21332#endif
333
334/*
drh2aae3a92023-12-28 21:02:08335** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
336** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
337*/
338#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1
339 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
340# undef SQLITE_DIRECT_OVERFLOW_READ
341#else
342 /* In all other cases, enable */
343# define SQLITE_DIRECT_OVERFLOW_READ 1
344#endif
345
346
347/*
drh38c67c32010-09-08 02:30:27348** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
349** 0 means mutexes are permanently disable and the library is never
350** threadsafe. 1 means the library is serialized which is the highest
drhf7b54962013-05-28 12:11:54351** level of threadsafety. 2 means the library is multithreaded - multiple
drh38c67c32010-09-08 02:30:27352** threads can use SQLite as long as no two threads try to use the same
353** database connection at the same time.
354**
drhe5e7a902007-10-01 17:47:00355** Older versions of SQLite used an optional THREADSAFE macro.
drh38c67c32010-09-08 02:30:27356** We support that for legacy.
dan814aad62017-06-17 17:29:24357**
358** To ensure that the correct value of "THREADSAFE" is reported when querying
359** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
360** logic is partially replicated in ctime.c. If it is updated here, it should
361** also be updated there.
drhe5e7a902007-10-01 17:47:00362*/
363#if !defined(SQLITE_THREADSAFE)
drhd16d0bc2013-04-16 18:24:34364# if defined(THREADSAFE)
365# define SQLITE_THREADSAFE THREADSAFE
366# else
367# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
368# endif
drhe5e7a902007-10-01 17:47:00369#endif
370
drh67080612007-10-01 14:30:14371/*
drhcb15f352011-12-23 01:04:17372** Powersafe overwrite is on by default. But can be turned off using
373** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
374*/
375#ifndef SQLITE_POWERSAFE_OVERWRITE
376# define SQLITE_POWERSAFE_OVERWRITE 1
377#endif
378
379/*
drhd1dcb232014-11-01 18:32:18380** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
381** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
382** which case memory allocation statistics are disabled by default.
danielk19770a732f52008-09-04 17:17:38383*/
384#if !defined(SQLITE_DEFAULT_MEMSTATUS)
385# define SQLITE_DEFAULT_MEMSTATUS 1
386#endif
387
388/*
drh0d180202008-02-14 23:26:56389** Exactly one of the following macros must be defined in order to
390** specify which memory allocation subsystem to use.
391**
392** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
mistachkin1b186a92011-08-24 16:13:57393** SQLITE_WIN32_MALLOC // Use Win32 native heap API
drhd1b0afc2012-06-21 14:25:17394** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
drh0d180202008-02-14 23:26:56395** SQLITE_MEMDEBUG // Debugging version of system malloc()
drha2460e02010-01-14 00:39:26396**
mistachkin753c5442011-08-25 02:02:25397** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
398** assert() macro is enabled, each call into the Win32 native heap subsystem
399** will cause HeapValidate to be called. If heap validation should fail, an
400** assertion will be triggered.
401**
drh0d180202008-02-14 23:26:56402** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
403** the default.
404*/
drhd1b0afc2012-06-21 14:25:17405#if defined(SQLITE_SYSTEM_MALLOC) \
406 + defined(SQLITE_WIN32_MALLOC) \
407 + defined(SQLITE_ZERO_MALLOC) \
408 + defined(SQLITE_MEMDEBUG)>1
409# error "Two or more of the following compile-time configuration options\
mistachkin20b1ff02012-06-21 15:12:30410 are defined but at most one is allowed:\
drhd1b0afc2012-06-21 14:25:17411 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
412 SQLITE_ZERO_MALLOC"
drh0d180202008-02-14 23:26:56413#endif
drhd1b0afc2012-06-21 14:25:17414#if defined(SQLITE_SYSTEM_MALLOC) \
415 + defined(SQLITE_WIN32_MALLOC) \
416 + defined(SQLITE_ZERO_MALLOC) \
417 + defined(SQLITE_MEMDEBUG)==0
drh0d180202008-02-14 23:26:56418# define SQLITE_SYSTEM_MALLOC 1
419#endif
420
421/*
drh8a1e5942009-04-28 15:43:45422** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
drheee4c8c2008-02-18 22:24:57423** sizes of memory allocations below this value where possible.
424*/
drh8a1e5942009-04-28 15:43:45425#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
drheee4c8c2008-02-18 22:24:57426# define SQLITE_MALLOC_SOFT_LIMIT 1024
427#endif
428
429/*
drh67080612007-10-01 14:30:14430** We need to define _XOPEN_SOURCE as follows in order to enable
drh40b521f2013-05-24 12:47:26431** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
432** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
433** it.
drh67080612007-10-01 14:30:14434*/
drh40b521f2013-05-24 12:47:26435#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
436# define _XOPEN_SOURCE 600
drh67080612007-10-01 14:30:14437#endif
danielk1977e3026632004-06-22 11:29:02438
drh7d10d5a2008-08-20 16:35:10439/*
drh1b28b892012-05-29 19:25:20440** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
441** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
442** make it true by defining or undefining NDEBUG.
443**
drh443dbcf2013-07-29 15:54:06444** Setting NDEBUG makes the code smaller and faster by disabling the
445** assert() statements in the code. So we want the default action
drh1b28b892012-05-29 19:25:20446** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
447** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
drh4b529d92005-09-13 00:00:00448** feature.
449*/
mistachkinbfc9b3f2016-02-15 22:01:24450#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
drh4b529d92005-09-13 00:00:00451# define NDEBUG 1
452#endif
drh1b28b892012-05-29 19:25:20453#if defined(NDEBUG) && defined(SQLITE_DEBUG)
454# undef NDEBUG
455#endif
drh4b529d92005-09-13 00:00:00456
drh64022502009-01-09 14:11:04457/*
drhc7379ce2013-10-30 02:28:23458** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
459*/
460#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
461# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
462#endif
463
464/*
mistachkinbfc9b3f2016-02-15 22:01:24465** The testcase() macro is used to aid in coverage testing. When
drh47c3b3e2009-01-10 16:15:20466** doing coverage testing, the condition inside the argument to
467** testcase() must be evaluated both true and false in order to
468** get full branch coverage. The testcase() macro is inserted
469** to help ensure adequate test coverage in places where simple
470** condition/decision coverage is inadequate. For example, testcase()
471** can be used to make sure boundary values are tested. For
472** bitmask tests, testcase() can be used to make sure each bit
473** is significant and used at least once. On switch statements
474** where multiple cases go to the same block of code, testcase()
475** can insure that all cases are evaluated.
drh47c3b3e2009-01-10 16:15:20476*/
drh9fdd66e2021-10-20 17:58:33477#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
478# ifndef SQLITE_AMALGAMATION
479 extern unsigned int sqlite3CoverageCounter;
480# endif
481# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; }
drh47c3b3e2009-01-10 16:15:20482#else
483# define testcase(X)
drh8f941bc2009-01-14 23:03:40484#endif
485
486/*
487** The TESTONLY macro is used to enclose variable declarations or
488** other bits of code that are needed to support the arguments
489** within testcase() and assert() macros.
490*/
491#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
492# define TESTONLY(X) X
493#else
drh47c3b3e2009-01-10 16:15:20494# define TESTONLY(X)
495#endif
496
497/*
drh61495262009-04-22 15:32:59498** Sometimes we need a small amount of code such as a variable initialization
499** to setup for a later assert() statement. We do not want this code to
500** appear when assert() is disabled. The following macro is therefore
501** used to contain that setup code. The "VVA" acronym stands for
502** "Verification, Validation, and Accreditation". In other words, the
503** code within VVA_ONLY() will only run during verification processes.
504*/
505#ifndef NDEBUG
506# define VVA_ONLY(X) X
507#else
508# define VVA_ONLY(X)
509#endif
510
511/*
drh11a9ad52021-10-04 18:21:14512** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
513** and mutation testing
514*/
drh16a8f282021-10-06 10:36:56515#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
drh11a9ad52021-10-04 18:21:14516# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
517#endif
518
519/*
mistachkinbfc9b3f2016-02-15 22:01:24520** The ALWAYS and NEVER macros surround boolean expressions which
drh47c3b3e2009-01-10 16:15:20521** are intended to always be true or false, respectively. Such
522** expressions could be omitted from the code completely. But they
523** are included in a few cases in order to enhance the resilience
524** of SQLite to unexpected behavior - to make the code "self-healing"
525** or "ductile" rather than being "brittle" and crashing at the first
526** hint of unplanned behavior.
527**
528** In other words, ALWAYS and NEVER are added for defensive code.
529**
530** When doing coverage testing ALWAYS and NEVER are hard-coded to
drh443dbcf2013-07-29 15:54:06531** be true and false so that the unreachable code they specify will
drh47c3b3e2009-01-10 16:15:20532** not be counted as untested code.
533*/
drh11a9ad52021-10-04 18:21:14534#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
drh47c3b3e2009-01-10 16:15:20535# define ALWAYS(X) (1)
536# define NEVER(X) (0)
537#elif !defined(NDEBUG)
drh2de80f42009-06-24 10:26:32538# define ALWAYS(X) ((X)?1:(assert(0),0))
539# define NEVER(X) ((X)?(assert(0),1):0)
drh47c3b3e2009-01-10 16:15:20540#else
541# define ALWAYS(X) (X)
542# define NEVER(X) (X)
543#endif
544
545/*
drh2f65b2f2017-10-02 21:29:51546** Some conditionals are optimizations only. In other words, if the
547** conditionals are replaced with a constant 1 (true) or 0 (false) then
548** the correct answer is still obtained, though perhaps not as quickly.
549**
550** The following macros mark these optimizations conditionals.
551*/
552#if defined(SQLITE_MUTATION_TEST)
553# define OK_IF_ALWAYS_TRUE(X) (1)
554# define OK_IF_ALWAYS_FALSE(X) (0)
555#else
556# define OK_IF_ALWAYS_TRUE(X) (X)
557# define OK_IF_ALWAYS_FALSE(X) (X)
558#endif
559
560/*
drhdad300d2016-01-18 00:20:26561** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
562** defined. We need to defend against those failures when testing with
563** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
564** during a normal build. The following macro can be used to disable tests
565** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
566*/
567#if defined(SQLITE_TEST_REALLOC_STRESS)
568# define ONLY_IF_REALLOC_STRESS(X) (X)
569#elif !defined(NDEBUG)
570# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
571#else
572# define ONLY_IF_REALLOC_STRESS(X) (0)
573#endif
574
575/*
mistachkin0cbcffa2015-04-16 03:56:32576** Declarations used for tracing the operating system interfaces.
577*/
mistachkinb10f22a2015-04-16 16:27:29578#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
579 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
mistachkin0cbcffa2015-04-16 03:56:32580 extern int sqlite3OSTrace;
581# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
582# define SQLITE_HAVE_OS_TRACE
583#else
584# define OSTRACE(X)
585# undef SQLITE_HAVE_OS_TRACE
586#endif
587
588/*
mistachkin5824d442015-04-28 23:34:10589** Is the sqlite3ErrName() function needed in the build? Currently,
590** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
591** OSTRACE is enabled), and by several "test*.c" files (which are
592** compiled using SQLITE_TEST).
593*/
594#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
595 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
596# define SQLITE_NEED_ERR_NAME
597#else
598# undef SQLITE_NEED_ERR_NAME
599#endif
600
601/*
drh7c621fb2016-03-09 13:39:43602** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
603*/
604#ifdef SQLITE_OMIT_EXPLAIN
605# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
606#endif
607
608/*
dan37f3ac82021-10-01 20:39:50609** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
610*/
611#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
612# define SQLITE_OMIT_ALTERTABLE
613#endif
614
dan3eae6662024-01-20 16:18:04615#define SQLITE_DIGIT_SEPARATOR '_'
616
dan37f3ac82021-10-01 20:39:50617/*
peter.d.reid60ec9142014-09-06 16:39:46618** Return true (non-zero) if the input is an integer that is too large
drh3e8e7ec2010-07-07 13:43:19619** to fit in 32-bits. This macro is used inside of various testcase()
620** macros to verify that we have tested SQLite for large-file support.
621*/
danb31a6af2010-07-14 06:20:26622#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
drh3e8e7ec2010-07-07 13:43:19623
624/*
drh47c3b3e2009-01-10 16:15:20625** The macro unlikely() is a hint that surrounds a boolean
626** expression that is usually false. Macro likely() surrounds
drh443dbcf2013-07-29 15:54:06627** a boolean expression that is usually true. These hints could,
628** in theory, be used by the compiler to generate better code, but
629** currently they are just comments for human readers.
drh47c3b3e2009-01-10 16:15:20630*/
drh443dbcf2013-07-29 15:54:06631#define likely(X) (X)
632#define unlikely(X) (X)
drh47c3b3e2009-01-10 16:15:20633
drhbeae3192001-09-22 18:12:08634#include "hash.h"
drh75897232000-05-29 14:26:00635#include "parse.h"
drh75897232000-05-29 14:26:00636#include <stdio.h>
637#include <stdlib.h>
638#include <string.h>
639#include <assert.h>
drh52370e22005-01-21 21:31:40640#include <stddef.h>
drh51bbf0c2024-10-03 16:31:08641#include <ctype.h>
drh75897232000-05-29 14:26:00642
drh967e8b72000-06-21 13:59:10643/*
drh8674e492017-01-19 21:20:11644** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
645** This allows better measurements of where memcpy() is used when running
646** cachegrind. But this macro version of memcpy() is very slow so it
647** should not be used in production. This is a performance measurement
648** hack only.
649*/
650#ifdef SQLITE_INLINE_MEMCPY
651# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
652 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
653#endif
654
655/*
drhb37df7b2005-10-13 02:09:49656** If compiling for a processor that lacks floating point support,
657** substitute integer for floating-point
658*/
659#ifdef SQLITE_OMIT_FLOATING_POINT
660# define double sqlite_int64
drh8b307fb2010-04-06 15:57:05661# define float sqlite_int64
drh6cb12332024-08-30 16:43:36662# define fabs(X) ((X)<0?-(X):(X))
663# define sqlite3IsOverflow(X) 0
drhd1167392006-01-23 13:00:35664# ifndef SQLITE_BIG_DBL
drhcdaca552009-08-20 13:45:07665# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
drhd1167392006-01-23 13:00:35666# endif
drhb37df7b2005-10-13 02:09:49667# define SQLITE_OMIT_DATETIME_FUNCS 1
668# define SQLITE_OMIT_TRACE 1
drh110daac2007-05-04 11:59:31669# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
drh0b3bf922009-06-15 20:45:34670# undef SQLITE_HAVE_ISNAN
drhb37df7b2005-10-13 02:09:49671#endif
drhd1167392006-01-23 13:00:35672#ifndef SQLITE_BIG_DBL
673# define SQLITE_BIG_DBL (1e99)
674#endif
drhb37df7b2005-10-13 02:09:49675
676/*
danielk197753c0f742005-03-29 03:10:59677** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
mistachkinbfc9b3f2016-02-15 22:01:24678** afterward. Having this macro allows us to cause the C compiler
danielk197753c0f742005-03-29 03:10:59679** to omit code used by TEMP tables without messy #ifndef statements.
680*/
681#ifdef SQLITE_OMIT_TEMPDB
682#define OMIT_TEMPDB 1
683#else
684#define OMIT_TEMPDB 0
685#endif
686
687/*
drhd946db02005-12-29 19:23:06688** The "file format" number is an integer that is incremented whenever
689** the VDBE-level file format changes. The following macros define the
690** the default file format for new databases and the maximum file format
691** that the library can read.
692*/
693#define SQLITE_MAX_FILE_FORMAT 4
694#ifndef SQLITE_DEFAULT_FILE_FORMAT
drhbf3f5f82011-11-07 13:05:23695# define SQLITE_DEFAULT_FILE_FORMAT 4
drhd946db02005-12-29 19:23:06696#endif
697
drha2460e02010-01-14 00:39:26698/*
699** Determine whether triggers are recursive by default. This can be
700** changed at run-time using a pragma.
701*/
dan5bde73c2009-09-01 17:11:07702#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
703# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
704#endif
705
drhd946db02005-12-29 19:23:06706/*
danielk1977b06a0b62008-06-26 10:54:12707** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
drh49766d62005-01-08 18:42:28708** on the command-line
709*/
danielk1977b06a0b62008-06-26 10:54:12710#ifndef SQLITE_TEMP_STORE
711# define SQLITE_TEMP_STORE 1
drh49766d62005-01-08 18:42:28712#endif
713
714/*
danb3f56fd2014-03-31 19:57:34715** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
mistachkinbfc9b3f2016-02-15 22:01:24716** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
danb3f56fd2014-03-31 19:57:34717** to zero.
718*/
drh6b2129a2014-08-29 19:06:07719#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
danb3f56fd2014-03-31 19:57:34720# undef SQLITE_MAX_WORKER_THREADS
drh028696c2014-08-25 23:44:44721# define SQLITE_MAX_WORKER_THREADS 0
danb3f56fd2014-03-31 19:57:34722#endif
723#ifndef SQLITE_MAX_WORKER_THREADS
drh6b2129a2014-08-29 19:06:07724# define SQLITE_MAX_WORKER_THREADS 8
danb3f56fd2014-03-31 19:57:34725#endif
drha09c8852014-05-03 11:22:09726#ifndef SQLITE_DEFAULT_WORKER_THREADS
727# define SQLITE_DEFAULT_WORKER_THREADS 0
728#endif
729#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
730# undef SQLITE_MAX_WORKER_THREADS
731# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
732#endif
danb3f56fd2014-03-31 19:57:34733
drh4297c7c2015-07-07 21:14:42734/*
735** The default initial allocation for the pagecache when using separate
736** pagecaches for each database connection. A positive number is the
737** number of pages. A negative number N translations means that a buffer
738** of -1024*N bytes is allocated and used for as many pages as it will hold.
drh83a4f472017-01-02 18:40:03739**
drh14851b92021-11-27 12:03:51740** The default value of "20" was chosen to minimize the run-time of the
drh83a4f472017-01-02 18:40:03741** speedtest1 test program with options: --shrink-memory --reprepare
drh4297c7c2015-07-07 21:14:42742*/
743#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
drh83a4f472017-01-02 18:40:03744# define SQLITE_DEFAULT_PCACHE_INITSZ 20
drh4297c7c2015-07-07 21:14:42745#endif
746
danb3f56fd2014-03-31 19:57:34747/*
dan2e3a5a82018-04-16 21:12:42748** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
749*/
750#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
751# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
752#endif
753
754/*
larrybrbc917382023-06-07 08:40:31755** The compile-time options SQLITE_MMAP_READWRITE and
drh2df94782017-07-22 16:32:33756** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
757** You must choose one or the other (or neither) but not both.
758*/
759#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
760#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
761#endif
762
763/*
drhf1974842004-11-05 03:56:00764** GCC does not define the offsetof() macro so we'll have to do it
765** ourselves.
766*/
767#ifndef offsetof
drh8ae57fa2025-05-30 15:43:04768# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0))
drhf1974842004-11-05 03:56:00769#endif
770
771/*
drhcebf06c2025-03-14 18:10:02772** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
773** to avoid complaints from -fsanitize=strict-bounds.
774*/
775#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
776# define FLEXARRAY
777#else
778# define FLEXARRAY 1
779#endif
780
781/*
drhe1e2e9a2013-06-13 15:16:53782** Macros to compute minimum and maximum of two numbers.
783*/
drh13969f52016-03-21 22:28:51784#ifndef MIN
785# define MIN(A,B) ((A)<(B)?(A):(B))
786#endif
787#ifndef MAX
788# define MAX(A,B) ((A)>(B)?(A):(B))
789#endif
drhe1e2e9a2013-06-13 15:16:53790
791/*
drh4fa4a542014-09-30 12:33:33792** Swap two objects of type TYPE.
793*/
794#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
795
796/*
drh9b8f4472006-04-04 01:54:55797** Check to see if this machine uses EBCDIC. (Yes, believe it or
798** not, there are still machines out there that use EBCDIC.)
799*/
800#if 'A' == '\301'
801# define SQLITE_EBCDIC 1
802#else
803# define SQLITE_ASCII 1
804#endif
805
806/*
drh5a2c2c22001-11-21 02:21:11807** Integers of known sizes. These typedefs might change for architectures
808** where the sizes very. Preprocessor macros are available so that the
809** types can be conveniently redefined at compile-type. Like this:
810**
811** cc '-DUINTPTR_TYPE=long long int' ...
drh41a2b482001-01-20 19:52:49812*/
drh5a2c2c22001-11-21 02:21:11813#ifndef UINT32_TYPE
mlcreechdda5b682008-03-14 13:02:08814# ifdef HAVE_UINT32_T
815# define UINT32_TYPE uint32_t
816# else
817# define UINT32_TYPE unsigned int
818# endif
drh5a2c2c22001-11-21 02:21:11819#endif
820#ifndef UINT16_TYPE
mlcreechdda5b682008-03-14 13:02:08821# ifdef HAVE_UINT16_T
822# define UINT16_TYPE uint16_t
823# else
824# define UINT16_TYPE unsigned short int
825# endif
drh5a2c2c22001-11-21 02:21:11826#endif
drh939a16d2004-07-15 13:37:22827#ifndef INT16_TYPE
mlcreechdda5b682008-03-14 13:02:08828# ifdef HAVE_INT16_T
829# define INT16_TYPE int16_t
830# else
831# define INT16_TYPE short int
832# endif
drh939a16d2004-07-15 13:37:22833#endif
drh5a2c2c22001-11-21 02:21:11834#ifndef UINT8_TYPE
mlcreechdda5b682008-03-14 13:02:08835# ifdef HAVE_UINT8_T
836# define UINT8_TYPE uint8_t
837# else
838# define UINT8_TYPE unsigned char
839# endif
drh5a2c2c22001-11-21 02:21:11840#endif
drh905793e2004-02-21 13:31:09841#ifndef INT8_TYPE
mlcreechdda5b682008-03-14 13:02:08842# ifdef HAVE_INT8_T
843# define INT8_TYPE int8_t
844# else
845# define INT8_TYPE signed char
846# endif
drh905793e2004-02-21 13:31:09847#endif
drhefad9992004-06-22 12:13:55848typedef sqlite_int64 i64; /* 8-byte signed integer */
drh27436af2006-03-28 23:57:17849typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
drh5a2c2c22001-11-21 02:21:11850typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
851typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
drh939a16d2004-07-15 13:37:22852typedef INT16_TYPE i16; /* 2-byte signed integer */
drh5a2c2c22001-11-21 02:21:11853typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
drh70a8ca32008-08-21 18:49:27854typedef INT8_TYPE i8; /* 1-byte signed integer */
drh5a2c2c22001-11-21 02:21:11855
drh03c65172025-02-08 13:34:19856/* A bitfield type for use inside of structures. Always follow with :N where
857** N is the number of bits.
858*/
859typedef unsigned bft; /* Bit Field Type */
860
drh5a2c2c22001-11-21 02:21:11861/*
drh35cd6432009-06-05 14:17:21862** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
863** that can be stored in a u32 without loss of data. The value
864** is 0x00000000ffffffff. But because of quirks of some compilers, we
865** have to specify the value in the less intuitive manner shown:
866*/
867#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
868
869/*
drhfaacf172011-08-12 01:51:45870** The datatype used to store estimates of the number of rows in a
drh03b30b72023-02-08 17:28:42871** table or index.
drhfaacf172011-08-12 01:51:45872*/
drh03b30b72023-02-08 17:28:42873typedef u64 tRowcnt;
drhfaacf172011-08-12 01:51:45874
875/*
drhbf539c42013-10-05 18:16:02876** Estimated quantities used for query planning are stored as 16-bit
877** logarithms. For quantity X, the value stored is 10*log2(X). This
878** gives a possible range of values of approximately 1.0e986 to 1e-986.
879** But the allowed values are "grainy". Not every value is representable.
880** For example, quantities 16 and 17 are both represented by a LogEst
drh97d38982014-11-07 14:37:32881** of 40. However, since LogEst quantities are suppose to be estimates,
drhbf539c42013-10-05 18:16:02882** not exact values, this imprecision is not a problem.
883**
drh224155d2014-04-30 13:19:09884** "LogEst" is short for "Logarithmic Estimate".
drhbf539c42013-10-05 18:16:02885**
886** Examples:
887** 1 -> 0 20 -> 43 10000 -> 132
888** 2 -> 10 25 -> 46 25000 -> 146
889** 3 -> 16 100 -> 66 1000000 -> 199
890** 4 -> 20 1000 -> 99 1048576 -> 200
891** 10 -> 33 1024 -> 100 4294967296 -> 320
892**
mistachkinbfc9b3f2016-02-15 22:01:24893** The LogEst can be negative to indicate fractional values.
drhbf539c42013-10-05 18:16:02894** Examples:
895**
896** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
897*/
898typedef INT16_TYPE LogEst;
drhfe54b7a2025-01-26 17:29:33899#define LOGEST_MIN (-32768)
900#define LOGEST_MAX (32767)
drhbf539c42013-10-05 18:16:02901
902/*
drh2b4905c2015-03-23 18:52:56903** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
904*/
905#ifndef SQLITE_PTRSIZE
906# if defined(__SIZEOF_POINTER__)
907# define SQLITE_PTRSIZE __SIZEOF_POINTER__
908# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
mistachkin88edc6c2018-08-24 19:04:08909 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
drh4c11a522024-01-28 20:42:12910 (defined(__APPLE__) && defined(__ppc__)) || \
mistachkin88edc6c2018-08-24 19:04:08911 (defined(__TOS_AIX__) && !defined(__64BIT__))
drh2b4905c2015-03-23 18:52:56912# define SQLITE_PTRSIZE 4
913# else
914# define SQLITE_PTRSIZE 8
915# endif
916#endif
917
drh3bfa7e82016-03-22 14:37:59918/* The uptr type is an unsigned integer large enough to hold a pointer
919*/
920#if defined(HAVE_STDINT_H)
921 typedef uintptr_t uptr;
922#elif SQLITE_PTRSIZE==4
923 typedef u32 uptr;
924#else
925 typedef u64 uptr;
926#endif
927
928/*
929** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
930** something between S (inclusive) and E (exclusive).
931**
932** In other words, S is a buffer and E is a pointer to the first byte after
933** the end of buffer S. This macro returns true if P points to something
934** contained within the buffer S.
935*/
drhbc6d9492023-07-13 14:49:39936#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
drh3bfa7e82016-03-22 14:37:59937
drhbc6d9492023-07-13 14:49:39938/*
939** P is one byte past the end of a large buffer. Return true if a span of bytes
940** between S..E crosses the end of that buffer. In other words, return true
drh80c43862023-08-08 17:36:03941** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
drhbc6d9492023-07-13 14:49:39942**
943** S is the start of the span. E is one byte past the end of end of span.
944**
945** P
946** |-----------------| FALSE
947** |-------|
948** S E
949**
950** P
951** |-----------------|
952** |-------| TRUE
953** S E
954**
955** P
956** |-----------------|
957** |-------| FALSE
958** S E
959*/
960#define SQLITE_OVERFLOW(P,S,E) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))
drh3bfa7e82016-03-22 14:37:59961
drh2b4905c2015-03-23 18:52:56962/*
drhbbd42a62004-05-22 17:41:58963** Macros to determine whether the machine is big or little endian,
drh71794db2014-04-18 00:49:29964** and whether or not that determination is run-time or compile-time.
965**
966** For best performance, an attempt is made to guess at the byte-order
967** using C-preprocessor macros. If that is unsuccessful, or if
drha39284b2017-02-09 17:12:22968** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
drh71794db2014-04-18 00:49:29969** at run-time.
drh25a6e6e2023-09-04 12:50:17970**
971** If you are building SQLite on some obscure platform for which the
972** following ifdef magic does not work, you can always include either:
973**
974** -DSQLITE_BYTEORDER=1234
975**
976** or
977**
978** -DSQLITE_BYTEORDER=4321
979**
980** to cause the build to work for little-endian or big-endian processors,
981** respectively.
drhbbd42a62004-05-22 17:41:58982*/
drh25a6e6e2023-09-04 12:50:17983#ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
984# if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
985# define SQLITE_BYTEORDER 4321
986# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
987# define SQLITE_BYTEORDER 1234
988# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
989# define SQLITE_BYTEORDER 4321
990# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
drhacd6bb52019-05-20 18:43:57991 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
992 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
993 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
drh25a6e6e2023-09-04 12:50:17994# define SQLITE_BYTEORDER 1234
995# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
996# define SQLITE_BYTEORDER 4321
drha39284b2017-02-09 17:12:22997# else
998# define SQLITE_BYTEORDER 0
999# endif
drh71794db2014-04-18 00:49:291000#endif
drha39284b2017-02-09 17:12:221001#if SQLITE_BYTEORDER==4321
drh71794db2014-04-18 00:49:291002# define SQLITE_BIGENDIAN 1
1003# define SQLITE_LITTLEENDIAN 0
1004# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
drha39284b2017-02-09 17:12:221005#elif SQLITE_BYTEORDER==1234
1006# define SQLITE_BIGENDIAN 0
1007# define SQLITE_LITTLEENDIAN 1
1008# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
1009#else
drhe1462a72015-12-24 14:53:271010# ifdef SQLITE_AMALGAMATION
1011 const int sqlite3one = 1;
1012# else
1013 extern const int sqlite3one;
1014# endif
drh38def052007-03-31 15:27:591015# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
1016# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
drh2cf4acb2014-04-18 00:06:021017# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
drh38def052007-03-31 15:27:591018#endif
drhbbd42a62004-05-22 17:41:581019
1020/*
drh0f050352008-05-09 18:03:131021** Constants for the largest and smallest possible 64-bit signed integers.
1022** These macros are designed to work correctly on both 32-bit and 64-bit
1023** compilers.
1024*/
1025#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
drhd1d89142020-07-06 12:13:051026#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))
drh0f050352008-05-09 18:03:131027#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
1028
mistachkinbfc9b3f2016-02-15 22:01:241029/*
drhef86b942025-02-17 17:33:141030** Macro SMXV(n) return the maximum value that can be held in variable n,
1031** assuming n is a signed integer type. UMXV(n) is similar for unsigned
1032** integer types.
1033*/
drhc52e9d92025-06-27 19:02:211034#define SMXV(n) ((((i64)1)<<(sizeof(n)*8-1))-1)
1035#define UMXV(n) ((((i64)1)<<(sizeof(n)*8))-1)
drhef86b942025-02-17 17:33:141036
1037/*
danielk1977be229652009-03-20 14:18:511038** Round up a number to the next larger multiple of 8. This is used
1039** to force 8-byte alignment on 64-bit architectures.
drhcf6e3fd2022-04-01 18:45:111040**
1041** ROUND8() always does the rounding, for any argument.
1042**
1043** ROUND8P() assumes that the argument is already an integer number of
1044** pointers in size, and so it is a no-op on systems where the pointer
1045** size is 8.
danielk1977be229652009-03-20 14:18:511046*/
danielk1977bc739712009-03-23 04:33:321047#define ROUND8(x) (((x)+7)&~7)
drhcf6e3fd2022-04-01 18:45:111048#if SQLITE_PTRSIZE==8
1049# define ROUND8P(x) (x)
1050#else
1051# define ROUND8P(x) (((x)+7)&~7)
1052#endif
danielk1977bc739712009-03-23 04:33:321053
1054/*
1055** Round down to the nearest multiple of 8
1056*/
1057#define ROUNDDOWN8(x) ((x)&~7)
danielk1977be229652009-03-20 14:18:511058
drh0f050352008-05-09 18:03:131059/*
drh8e14c592009-12-04 23:10:121060** Assert that the pointer X is aligned to an 8-byte boundary. This
1061** macro is used only within assert() to verify that the code gets
1062** all alignment restrictions correct.
1063**
1064** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
peter.d.reid60ec9142014-09-06 16:39:461065** underlying malloc() implementation might return us 4-byte aligned
drh8e14c592009-12-04 23:10:121066** pointers. In that case, only verify 4-byte alignment.
drhea598cb2009-04-05 12:22:081067*/
drh8e14c592009-12-04 23:10:121068#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
drh3547e492022-12-23 14:49:241069# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
drh8e14c592009-12-04 23:10:121070#else
drh3547e492022-12-23 14:49:241071# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
drh8e14c592009-12-04 23:10:121072#endif
drhea598cb2009-04-05 12:22:081073
drh188d4882013-04-08 20:47:491074/*
drh9b4c59f2013-04-15 17:03:421075** Disable MMAP on platforms where it is known to not work
drh188d4882013-04-08 20:47:491076*/
1077#if defined(__OpenBSD__) || defined(__QNXNTO__)
drh9b4c59f2013-04-15 17:03:421078# undef SQLITE_MAX_MMAP_SIZE
1079# define SQLITE_MAX_MMAP_SIZE 0
drh188d4882013-04-08 20:47:491080#endif
1081
drh9b4c59f2013-04-15 17:03:421082/*
1083** Default maximum size of memory used by memory-mapped I/O in the VFS
1084*/
1085#ifdef __APPLE__
1086# include <TargetConditionals.h>
drh9b4c59f2013-04-15 17:03:421087#endif
1088#ifndef SQLITE_MAX_MMAP_SIZE
1089# if defined(__linux__) \
1090 || defined(_WIN32) \
1091 || (defined(__APPLE__) && defined(__MACH__)) \
drh3e9dd932015-07-15 23:15:591092 || defined(__sun) \
1093 || defined(__FreeBSD__) \
1094 || defined(__DragonFly__)
drh2bba8c22013-04-26 12:08:291095# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
drh9b4c59f2013-04-15 17:03:421096# else
1097# define SQLITE_MAX_MMAP_SIZE 0
1098# endif
1099#endif
1100
1101/*
1102** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
1103** default MMAP_SIZE is specified at compile-time, make sure that it does
1104** not exceed the maximum mmap size.
1105*/
1106#ifndef SQLITE_DEFAULT_MMAP_SIZE
1107# define SQLITE_DEFAULT_MMAP_SIZE 0
1108#endif
1109#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
1110# undef SQLITE_DEFAULT_MMAP_SIZE
1111# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
1112#endif
drhe7b347072009-06-01 18:18:201113
drhea598cb2009-04-05 12:22:081114/*
drh5e431be2022-04-06 11:08:381115** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not
1116** the Abstract Syntax Tree tracing logic is turned on.
drhabd4c722014-09-20 18:18:331117*/
drhc0622a42020-12-04 01:17:571118#if !defined(SQLITE_AMALGAMATION)
drh5e431be2022-04-06 11:08:381119extern u32 sqlite3TreeTrace;
drhabd4c722014-09-20 18:18:331120#endif
drhc0622a42020-12-04 01:17:571121#if defined(SQLITE_DEBUG) \
drh5e431be2022-04-06 11:08:381122 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
1123 || defined(SQLITE_ENABLE_TREETRACE))
1124# define TREETRACE_ENABLED 1
drh5d7aef12022-11-22 19:49:161125# define TREETRACE(K,P,S,X) \
drh5e431be2022-04-06 11:08:381126 if(sqlite3TreeTrace&(K)) \
drh9216de82020-06-11 00:57:091127 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
1128 sqlite3DebugPrintf X
1129#else
drh5d7aef12022-11-22 19:49:161130# define TREETRACE(K,P,S,X)
drh5e431be2022-04-06 11:08:381131# define TREETRACE_ENABLED 0
drh9216de82020-06-11 00:57:091132#endif
drhabd4c722014-09-20 18:18:331133
drhc7c5b8a2022-11-22 19:56:541134/* TREETRACE flag meanings:
1135**
1136** 0x00000001 Beginning and end of SELECT processing
1137** 0x00000002 WHERE clause processing
1138** 0x00000004 Query flattener
1139** 0x00000008 Result-set wildcard expansion
1140** 0x00000010 Query name resolution
1141** 0x00000020 Aggregate analysis
1142** 0x00000040 Window functions
1143** 0x00000080 Generated column names
1144** 0x00000100 Move HAVING terms into WHERE
1145** 0x00000200 Count-of-view optimization
1146** 0x00000400 Compound SELECT processing
1147** 0x00000800 Drop superfluous ORDER BY
1148** 0x00001000 LEFT JOIN simplifies to JOIN
1149** 0x00002000 Constant propagation
1150** 0x00004000 Push-down optimization
1151** 0x00008000 After all FROM-clause analysis
1152** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
1153** 0x00020000 Transform DISTINCT into GROUP BY
1154** 0x00040000 SELECT tree dump after all code has been generated
drh61b77a62024-03-08 21:37:181155** 0x00080000 NOT NULL strength reduction
drh6ed5aa42025-06-16 15:34:261156** 0x00100000 Pointers are all shown as zero
drhaa54d7a2025-07-02 20:46:021157** 0x00200000 EXISTS-to-JOIN optimization
drhc7c5b8a2022-11-22 19:56:541158*/
1159
drhabd4c722014-09-20 18:18:331160/*
drhc0622a42020-12-04 01:17:571161** Macros for "wheretrace"
1162*/
drh8e2b9c22020-12-20 14:51:171163extern u32 sqlite3WhereTrace;
drhc0622a42020-12-04 01:17:571164#if defined(SQLITE_DEBUG) \
1165 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
1166# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
1167# define WHERETRACE_ENABLED 1
1168#else
1169# define WHERETRACE(K,X)
1170#endif
1171
drh2a757652022-11-30 19:11:311172/*
1173** Bits for the sqlite3WhereTrace mask:
1174**
1175** (---any--) Top-level block structure
1176** 0x-------F High-level debug messages
1177** 0x----FFF- More detail
1178** 0xFFFF---- Low-level debug messages
1179**
1180** 0x00000001 Code generation
drh0186ee12025-01-25 14:30:361181** 0x00000002 Solver (Use 0x40000 for less detail)
drh2a757652022-11-30 19:11:311182** 0x00000004 Solver costs
1183** 0x00000008 WhereLoop inserts
1184**
1185** 0x00000010 Display sqlite3_index_info xBestIndex calls
1186** 0x00000020 Range an equality scan metrics
1187** 0x00000040 IN operator decisions
drh8ce73ce2024-04-02 14:12:291188** 0x00000080 WhereLoop cost adjustments
drh2a757652022-11-30 19:11:311189** 0x00000100
1190** 0x00000200 Covering index decisions
1191** 0x00000400 OR optimization
1192** 0x00000800 Index scanner
1193** 0x00001000 More details associated with code generation
1194** 0x00002000
1195** 0x00004000 Show all WHERE terms at key points
1196** 0x00008000 Show the full SELECT statement at key places
1197**
1198** 0x00010000 Show more detail when printing WHERE terms
1199** 0x00020000 Show WHERE terms returned from whereScanNext()
drh0186ee12025-01-25 14:30:361200** 0x00040000 Solver overview messages
1201** 0x00080000 Star-query heuristic
drh6ed5aa42025-06-16 15:34:261202** 0x00100000 Pointers are all shown as zero
drh2a757652022-11-30 19:11:311203*/
1204
drhc0622a42020-12-04 01:17:571205
1206/*
drh90f5ecb2004-07-22 01:19:351207** An instance of the following structure is used to store the busy-handler
mistachkinbfc9b3f2016-02-15 22:01:241208** callback for a given sqlite handle.
drh90f5ecb2004-07-22 01:19:351209**
1210** The sqlite.busyHandler member of the sqlite struct contains the busy
1211** callback for the database handle. Each pager opened via the sqlite
1212** handle is passed a pointer to sqlite.busyHandler. The busy-handler
1213** callback is currently invoked only from within pager.c.
1214*/
1215typedef struct BusyHandler BusyHandler;
1216struct BusyHandler {
drh80262892018-03-26 16:37:531217 int (*xBusyHandler)(void *,int); /* The busy callback */
1218 void *pBusyArg; /* First arg to busy callback */
1219 int nBusy; /* Incremented with each busy call */
drh90f5ecb2004-07-22 01:19:351220};
1221
1222/*
drhccb21132020-06-19 11:34:571223** Name of table that holds the database schema.
drha4a871c2021-11-04 14:04:201224**
larrybrbc917382023-06-07 08:40:311225** The PREFERRED names are used wherever possible. But LEGACY is also
drha4a871c2021-11-04 14:04:201226** used for backwards compatibility.
1227**
1228** 1. Queries can use either the PREFERRED or the LEGACY names
1229** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
1230** 3. The PRAGMA table_list statement uses the PREFERRED name
1231**
1232** The LEGACY names are stored in the internal symbol hash table
1233** in support of (2). Names are translated using sqlite3PreferredTableName()
1234** for (3). The sqlite3FindTable() function takes care of translating
1235** names for (1).
1236**
1237** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
drh75897232000-05-29 14:26:001238*/
drha4a871c2021-11-04 14:04:201239#define LEGACY_SCHEMA_TABLE "sqlite_master"
1240#define LEGACY_TEMP_SCHEMA_TABLE "sqlite_temp_master"
1241#define PREFERRED_SCHEMA_TABLE "sqlite_schema"
1242#define PREFERRED_TEMP_SCHEMA_TABLE "sqlite_temp_schema"
drh346a70c2020-06-15 20:27:351243
drh75897232000-05-29 14:26:001244
1245/*
drh346a70c2020-06-15 20:27:351246** The root-page of the schema table.
danielk19778e150812004-05-10 01:17:371247*/
drh346a70c2020-06-15 20:27:351248#define SCHEMA_ROOT 1
danielk19778e150812004-05-10 01:17:371249
1250/*
drh346a70c2020-06-15 20:27:351251** The name of the schema table. The name is different for TEMP.
drhed6c8672003-01-12 18:02:161252*/
drh346a70c2020-06-15 20:27:351253#define SCHEMA_TABLE(x) \
drha4a871c2021-11-04 14:04:201254 ((!OMIT_TEMPDB)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE:LEGACY_SCHEMA_TABLE)
drhed6c8672003-01-12 18:02:161255
1256/*
drh75897232000-05-29 14:26:001257** A convenience macro that returns the number of elements in
1258** an array.
1259*/
danielk197723432972008-11-17 16:42:001260#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
drh75897232000-05-29 14:26:001261
1262/*
drh7a5bcc02013-01-16 17:08:581263** Determine if the argument is a power of two
1264*/
1265#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
1266
1267/*
drh633e6d52008-07-28 19:34:531268** The following value as a destructor means to use sqlite3DbFree().
mistachkinbfc9b3f2016-02-15 22:01:241269** The sqlite3DbFree() routine requires two parameters instead of the
1270** one parameter that destructors normally want. So we have to introduce
1271** this magic value that the code knows to handle differently. Any
drhaa538a52012-01-19 16:57:161272** pointer will work here as long as it is distinct from SQLITE_STATIC
1273** and SQLITE_TRANSIENT.
drh633e6d52008-07-28 19:34:531274*/
drh9a9140b2025-06-18 14:14:461275#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3RowSetClear)
drh633e6d52008-07-28 19:34:531276
drh78f82d12008-09-02 00:52:521277/*
1278** When SQLITE_OMIT_WSD is defined, it means that the target platform does
1279** not support Writable Static Data (WSD) such as global and static variables.
1280** All variables must either be on the stack or dynamically allocated from
1281** the heap. When WSD is unsupported, the variable declarations scattered
1282** throughout the SQLite code must become constants instead. The SQLITE_WSD
1283** macro is used for this purpose. And instead of referencing the variable
1284** directly, we use its constant as a key to lookup the run-time allocated
1285** buffer that holds real variable. The constant is also the initializer
1286** for the run-time allocated buffer.
1287**
shane2479de32008-11-10 18:05:351288** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
drh78f82d12008-09-02 00:52:521289** macros become no-ops and have zero performance impact.
1290*/
danielk1977075c23a2008-09-01 18:34:201291#ifdef SQLITE_OMIT_WSD
1292 #define SQLITE_WSD const
1293 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
1294 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
drhd1d38482008-10-07 23:46:381295 int sqlite3_wsd_init(int N, int J);
1296 void *sqlite3_wsd_find(void *K, int L);
danielk1977075c23a2008-09-01 18:34:201297#else
mistachkinbfc9b3f2016-02-15 22:01:241298 #define SQLITE_WSD
danielk1977075c23a2008-09-01 18:34:201299 #define GLOBAL(t,v) v
1300 #define sqlite3GlobalConfig sqlite3Config
1301#endif
1302
danielk1977f3d3c272008-11-19 16:52:441303/*
1304** The following macros are used to suppress compiler warnings and to
mistachkinbfc9b3f2016-02-15 22:01:241305** make it clear to human readers when a function parameter is deliberately
danielk1977f3d3c272008-11-19 16:52:441306** left unused within the body of a function. This usually happens when
mistachkinbfc9b3f2016-02-15 22:01:241307** a function is called via a function pointer. For example the
danielk1977f3d3c272008-11-19 16:52:441308** implementation of an SQL aggregate step callback may not use the
1309** parameter indicating the number of arguments passed to the aggregate,
1310** if it knows that this is enforced elsewhere.
1311**
1312** When a function parameter is not used at all within the body of a function,
1313** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
1314** However, these macros may also be used to suppress warnings related to
1315** parameters that may or may not be used depending on compilation options.
1316** For example those parameters only used in assert() statements. In these
1317** cases the parameters are named as per the usual conventions.
1318*/
danielk197762c14b32008-11-19 09:05:261319#define UNUSED_PARAMETER(x) (void)(x)
danielk1977f3d3c272008-11-19 16:52:441320#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
danielk197762c14b32008-11-19 09:05:261321
drh633e6d52008-07-28 19:34:531322/*
drh75897232000-05-29 14:26:001323** Forward references to structures
1324*/
drh13449892005-09-07 21:22:451325typedef struct AggInfo AggInfo;
drhfe05af82005-07-21 03:14:591326typedef struct AuthContext AuthContext;
drh0b9f50d2009-06-23 20:28:531327typedef struct AutoincInfo AutoincInfo;
drhf5e7bb52008-02-18 14:47:331328typedef struct Bitvec Bitvec;
drhfe05af82005-07-21 03:14:591329typedef struct CollSeq CollSeq;
drh7020f652000-06-03 18:06:521330typedef struct Column Column;
drhf824b412021-02-20 14:57:161331typedef struct Cte Cte;
drha79e2a22021-02-21 23:44:141332typedef struct CteUse CteUse;
drhfe05af82005-07-21 03:14:591333typedef struct Db Db;
drh10deb352023-08-30 15:20:151334typedef struct DbClientData DbClientData;
danf380c3f2021-01-21 15:40:521335typedef struct DbFixer DbFixer;
danielk1977e501b892006-01-09 06:29:471336typedef struct Schema Schema;
drh75897232000-05-29 14:26:001337typedef struct Expr Expr;
1338typedef struct ExprList ExprList;
drhc2eef3b2002-08-31 18:53:061339typedef struct FKey FKey;
drha1b0ff12023-06-30 18:35:431340typedef struct FpDecode FpDecode;
dand2199f02010-08-27 17:48:521341typedef struct FuncDestructor FuncDestructor;
drhfe05af82005-07-21 03:14:591342typedef struct FuncDef FuncDef;
drh70a8ca32008-08-21 18:49:271343typedef struct FuncDefHash FuncDefHash;
drhfe05af82005-07-21 03:14:591344typedef struct IdList IdList;
1345typedef struct Index Index;
drhe70d4582022-10-17 14:46:391346typedef struct IndexedExpr IndexedExpr;
dan02fa4692009-08-17 17:06:581347typedef struct IndexSample IndexSample;
danielk19778d059842004-05-12 11:24:021348typedef struct KeyClass KeyClass;
drhd3d39e92004-05-20 22:16:291349typedef struct KeyInfo KeyInfo;
drh633e6d52008-07-28 19:34:531350typedef struct Lookaside Lookaside;
1351typedef struct LookasideSlot LookasideSlot;
danielk1977d1ab1ba2006-06-15 04:28:131352typedef struct Module Module;
drh626a8792005-01-17 22:08:191353typedef struct NameContext NameContext;
drhd44f8b22022-04-07 01:11:131354typedef struct OnOrUsing OnOrUsing;
drhfe05af82005-07-21 03:14:591355typedef struct Parse Parse;
drhcf3c0782021-01-11 20:37:021356typedef struct ParseCleanup ParseCleanup;
dan46c47d42011-03-01 18:42:071357typedef struct PreUpdate PreUpdate;
drha5c14162013-12-17 15:03:061358typedef struct PrintfArguments PrintfArguments;
drhf02cc9a2023-07-25 15:08:181359typedef struct RCStr RCStr;
dancf8f2892018-08-09 20:47:011360typedef struct RenameToken RenameToken;
drhb8352472021-01-29 19:32:171361typedef struct Returning Returning;
drha2460e02010-01-14 00:39:261362typedef struct RowSet RowSet;
danielk1977fd7f0452008-12-17 17:30:261363typedef struct Savepoint Savepoint;
drhfe05af82005-07-21 03:14:591364typedef struct Select Select;
drhf51446a2012-07-21 19:40:421365typedef struct SQLiteThread SQLiteThread;
drh634d81d2012-09-20 15:41:311366typedef struct SelectDest SelectDest;
drh1521ca42024-08-19 22:48:301367typedef struct Subquery Subquery;
drh76012942021-02-21 21:04:541368typedef struct SrcItem SrcItem;
drhfe05af82005-07-21 03:14:591369typedef struct SrcList SrcList;
drh0cdbe1a2018-05-09 13:46:261370typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
drhfe05af82005-07-21 03:14:591371typedef struct Table Table;
danielk1977c00da102006-01-07 13:21:041372typedef struct TableLock TableLock;
drhfe05af82005-07-21 03:14:591373typedef struct Token Token;
drh4fa4a542014-09-30 12:33:331374typedef struct TreeView TreeView;
drha2460e02010-01-14 00:39:261375typedef struct Trigger Trigger;
dan2832ad42009-08-31 15:27:271376typedef struct TriggerPrg TriggerPrg;
drhfe05af82005-07-21 03:14:591377typedef struct TriggerStep TriggerStep;
drhe63d9992008-08-13 19:11:481378typedef struct UnpackedRecord UnpackedRecord;
drh46d2e5c2018-04-12 13:15:431379typedef struct Upsert Upsert;
danielk1977595a5232009-07-24 17:58:531380typedef struct VTable VTable;
danb061d052011-04-25 18:49:571381typedef struct VtabCtx VtabCtx;
drh7d10d5a2008-08-20 16:35:101382typedef struct Walker Walker;
drhfe05af82005-07-21 03:14:591383typedef struct WhereInfo WhereInfo;
dan86fb6e12018-05-16 20:58:071384typedef struct Window Window;
dan7d562db2014-01-11 19:19:361385typedef struct With With;
drhd3d39e92004-05-20 22:16:291386
drh1fe3ac72018-06-09 01:12:081387
1388/*
1389** The bitmask datatype defined below is used for various optimizations.
1390**
1391** Changing this from a 64-bit to a 32-bit type limits the number of
1392** tables in a join to 32 instead of 64. But it also reduces the size
1393** of the library by 738 bytes on ix86.
1394*/
1395#ifdef SQLITE_BITMASK_TYPE
1396 typedef SQLITE_BITMASK_TYPE Bitmask;
1397#else
1398 typedef u64 Bitmask;
1399#endif
1400
1401/*
1402** The number of bits in a Bitmask. "BMS" means "BitMask Size".
1403*/
1404#define BMS ((int)(sizeof(Bitmask)*8))
1405
1406/*
1407** A bit in a Bitmask
1408*/
drh0fe7e7d2022-02-01 14:58:291409#define MASKBIT(n) (((Bitmask)1)<<(n))
1410#define MASKBIT64(n) (((u64)1)<<(n))
1411#define MASKBIT32(n) (((unsigned int)1)<<(n))
1412#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
1413#define ALLBITS ((Bitmask)-1)
drh54cc7662022-10-22 20:13:461414#define TOPBIT (((Bitmask)1)<<(BMS-1))
drh1fe3ac72018-06-09 01:12:081415
drh9bf755c2016-12-23 03:59:311416/* A VList object records a mapping between parameters/variables/wildcards
1417** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
1418** variable number associated with that parameter. See the format description
1419** on the sqlite3VListAdd() routine for more information. A VList is really
1420** just an array of integers.
1421*/
1422typedef int VList;
1423
danielk19772dca4ac2008-01-03 11:50:291424/*
mistachkinbfc9b3f2016-02-15 22:01:241425** Defer sourcing vdbe.h and btree.h until after the "u8" and
danielk19772dca4ac2008-01-03 11:50:291426** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
1427** pointer types (i.e. FuncDef) defined above.
1428*/
drh7585f492022-09-10 18:20:591429#include "os.h"
drhe9261db2020-07-20 12:47:321430#include "pager.h"
danielk19772dca4ac2008-01-03 11:50:291431#include "btree.h"
1432#include "vdbe.h"
danielk19778c0a7912008-08-20 14:49:231433#include "pcache.h"
drhc7ce76a2007-08-30 14:10:301434#include "mutex.h"
danielk19771cc5ed82007-05-16 17:28:431435
drh33b104a2016-03-08 16:07:591436/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
1437** synchronous setting to EXTRA. It is no longer supported.
1438*/
1439#ifdef SQLITE_EXTRA_DURABLE
1440# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
1441# define SQLITE_DEFAULT_SYNCHRONOUS 3
1442#endif
1443
drh50a1a5a2016-03-08 14:40:111444/*
drhc2ae2072016-03-08 15:30:011445** Default synchronous levels.
1446**
larrybrbc917382023-06-07 08:40:311447** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ
drhc2ae2072016-03-08 15:30:011448** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
1449**
1450** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
1451** OFF 1 0
1452** NORMAL 2 1
1453** FULL 3 2
1454** EXTRA 4 3
1455**
1456** The "PRAGMA synchronous" statement also uses the zero-based numbers.
1457** In other words, the zero-based numbers are used for all external interfaces
1458** and the one-based values are used internally.
drh50a1a5a2016-03-08 14:40:111459*/
1460#ifndef SQLITE_DEFAULT_SYNCHRONOUS
drh3e7d0122017-03-25 18:03:261461# define SQLITE_DEFAULT_SYNCHRONOUS 2
drh50a1a5a2016-03-08 14:40:111462#endif
1463#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
1464# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
1465#endif
drheee4c8c2008-02-18 22:24:571466
drh001bbcb2003-03-19 03:14:001467/*
1468** Each database file to be accessed by the system is an instance
1469** of the following structure. There are normally two of these structures
1470** in the sqlite.aDb[] array. aDb[0] is the main database file and
drha69d9162003-04-17 22:57:531471** aDb[1] is the database file used to hold temporary tables. Additional
1472** databases may be attached.
drh001bbcb2003-03-19 03:14:001473*/
1474struct Db {
drh69c33822016-08-18 14:33:111475 char *zDbSName; /* Name of this database. (schema name, not filename) */
drh001bbcb2003-03-19 03:14:001476 Btree *pBt; /* The B*Tree structure for this database file */
shane467bcf32008-11-24 20:01:321477 u8 safety_level; /* How aggressive at syncing data to disk */
drh50a1a5a2016-03-08 14:40:111478 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
danielk1977e501b892006-01-09 06:29:471479 Schema *pSchema; /* Pointer to database schema (possibly shared) */
danielk1977da184232006-01-05 11:34:321480};
1481
1482/*
1483** An instance of the following structure stores a database schema.
drh21206082011-04-04 18:22:021484**
1485** Most Schema objects are associated with a Btree. The exception is
larrybrbc917382023-06-07 08:40:311486** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
drh21206082011-04-04 18:22:021487** In shared cache mode, a single Schema object can be shared by multiple
1488** Btrees that refer to the same underlying BtShared object.
mistachkinbfc9b3f2016-02-15 22:01:241489**
drh21206082011-04-04 18:22:021490** Schema objects are automatically deallocated when the last Btree that
1491** references them is destroyed. The TEMP Schema is manually freed by
1492** sqlite3_close().
1493*
1494** A thread must be holding a mutex on the corresponding Btree in order
1495** to access Schema content. This implies that the thread must also be
1496** holding a mutex on the sqlite3 connection pointer that owns the Btree.
drh55a09592011-06-07 18:31:141497** For a TEMP Schema, only the connection mutex is required.
danielk1977da184232006-01-05 11:34:321498*/
danielk1977e501b892006-01-09 06:29:471499struct Schema {
drh001bbcb2003-03-19 03:14:001500 int schema_cookie; /* Database schema version number for this file */
drhc2a75552011-03-18 21:55:461501 int iGeneration; /* Generation counter. Incremented with each change */
drhd24cc422003-03-27 12:51:241502 Hash tblHash; /* All tables indexed by name */
1503 Hash idxHash; /* All (named) indices indexed by name */
1504 Hash trigHash; /* All triggers indexed by name */
dan1da40a32009-09-19 17:00:311505 Hash fkeyHash; /* All foreign keys by referenced table name */
drh4794f732004-11-05 17:17:501506 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
danielk1977da184232006-01-05 11:34:321507 u8 file_format; /* Schema format version for this file */
drh8079a0d2006-01-12 17:20:501508 u8 enc; /* Text encoding used by this database */
drh2c5e35f2014-08-05 11:04:211509 u16 schemaFlags; /* Flags associated with this schema */
danielk197714db2662006-01-09 16:12:041510 int cache_size; /* Number of pages to use in the cache */
drh001bbcb2003-03-19 03:14:001511};
drh75897232000-05-29 14:26:001512
1513/*
mistachkinbfc9b3f2016-02-15 22:01:241514** These macros can be used to test, set, or clear bits in the
drha2460e02010-01-14 00:39:261515** Db.pSchema->flags field.
drh8bf8dc92003-05-17 17:35:101516*/
drh2c5e35f2014-08-05 11:04:211517#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
1518#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
1519#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
1520#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
drh8bf8dc92003-05-17 17:35:101521
1522/*
drha2460e02010-01-14 00:39:261523** Allowed values for the DB.pSchema->flags field.
drh8bf8dc92003-05-17 17:35:101524**
drh8bf8dc92003-05-17 17:35:101525** The DB_SchemaLoaded flag is set after the database schema has been
1526** read into internal hash tables.
1527**
1528** DB_UnresetViews means that one or more views have column names that
1529** have been filled out. If the schema changes, these column names might
1530** changes and so the view will need to be reset.
1531*/
drh124b27e2004-06-19 16:06:101532#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
1533#define DB_UnresetViews 0x0002 /* Some views have defined column names */
drhdc6b41e2017-08-17 02:26:351534#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
drh8bf8dc92003-05-17 17:35:101535
drhcaa639f2008-03-20 00:32:201536/*
1537** The number of different kinds of things that can be limited
1538** using the sqlite3_limit() interface.
1539*/
drh111544c2014-08-29 16:20:471540#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
drh8bf8dc92003-05-17 17:35:101541
1542/*
drh633e6d52008-07-28 19:34:531543** Lookaside malloc is a set of fixed-size buffers that can be used
shane467bcf32008-11-24 20:01:321544** to satisfy small transient memory allocation requests for objects
drh633e6d52008-07-28 19:34:531545** associated with a particular database connection. The use of
1546** lookaside malloc provides a significant performance enhancement
1547** (approx 10%) by avoiding numerous malloc/free requests while parsing
1548** SQL statements.
1549**
1550** The Lookaside structure holds configuration information about the
1551** lookaside malloc subsystem. Each available memory allocation in
1552** the lookaside subsystem is stored on a linked list of LookasideSlot
1553** objects.
drhd9da78a2009-03-24 15:08:091554**
1555** Lookaside allocations are only allowed for objects that are associated
1556** with a particular database connection. Hence, schema information cannot
1557** be stored in lookaside because in shared cache mode the schema information
1558** is shared by multiple database connections. Therefore, while parsing
1559** schema information, the Lookaside.bEnabled flag is cleared so that
1560** lookaside allocations are not used to construct the schema objects.
drh31f69622019-10-05 14:39:361561**
1562** New lookaside allocations are only allowed if bDisable==0. When
1563** bDisable is greater than zero, sz is set to zero which effectively
1564** disables lookaside without adding a new test for the bDisable flag
1565** in a performance-critical path. sz should be set by to szTrue whenever
1566** bDisable changes back to zero.
drhe6068022019-12-13 15:48:211567**
1568** Lookaside buffers are initially held on the pInit list. As they are
1569** used and freed, they are added back to the pFree list. New allocations
1570** come off of pFree first, then pInit as a fallback. This dual-list
1571** allows use to compute a high-water mark - the maximum number of allocations
1572** outstanding at any point in the past - by subtracting the number of
1573** allocations on the pInit list from the total number of allocations.
1574**
drhcf014f62019-12-31 15:12:341575** Enhancement on 2019-12-12: Two-size-lookaside
drhe6068022019-12-13 15:48:211576** The default lookaside configuration is 100 slots of 1200 bytes each.
1577** The larger slot sizes are important for performance, but they waste
1578** a lot of space, as most lookaside allocations are less than 128 bytes.
drhcf014f62019-12-31 15:12:341579** The two-size-lookaside enhancement breaks up the lookaside allocation
1580** into two pools: One of 128-byte slots and the other of the default size
1581** (1200-byte) slots. Allocations are filled from the small-pool first,
drhe6068022019-12-13 15:48:211582** failing over to the full-size pool if that does not work. Thus more
1583** lookaside slots are available while also using less memory.
1584** This enhancement can be omitted by compiling with
drhcf014f62019-12-31 15:12:341585** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
drh633e6d52008-07-28 19:34:531586*/
1587struct Lookaside {
drh4a642b62016-02-05 01:55:271588 u32 bDisable; /* Only operate the lookaside when zero */
drh633e6d52008-07-28 19:34:531589 u16 sz; /* Size of each buffer in bytes */
drh31f69622019-10-05 14:39:361590 u16 szTrue; /* True value of sz, even if disabled */
drhe9d1c722008-08-04 20:13:261591 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
drh52fb8e12017-08-29 20:21:121592 u32 nSlot; /* Number of lookaside slots allocated */
1593 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
1594 LookasideSlot *pInit; /* List of buffers not previously used */
drh7d10d5a2008-08-20 16:35:101595 LookasideSlot *pFree; /* List of available buffers */
drhcf014f62019-12-31 15:12:341596#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
larrybrbc917382023-06-07 08:40:311597 LookasideSlot *pSmallInit; /* List of small buffers not previously used */
drhcf014f62019-12-31 15:12:341598 LookasideSlot *pSmallFree; /* List of available small buffers */
drhe6068022019-12-13 15:48:211599 void *pMiddle; /* First byte past end of full-size buffers and
drhcf014f62019-12-31 15:12:341600 ** the first byte of LOOKASIDE_SMALL buffers */
1601#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drh633e6d52008-07-28 19:34:531602 void *pStart; /* First byte of available memory space */
1603 void *pEnd; /* First byte past end of available space */
drh376860b2022-08-22 15:18:371604 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
drh633e6d52008-07-28 19:34:531605};
1606struct LookasideSlot {
1607 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
1608};
1609
drh31f69622019-10-05 14:39:361610#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
1611#define EnableLookaside db->lookaside.bDisable--;\
1612 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
1613
larrybrbc917382023-06-07 08:40:311614/* Size of the smaller allocations in two-size lookaside */
drhcf014f62019-12-31 15:12:341615#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
1616# define LOOKASIDE_SMALL 0
drhe6068022019-12-13 15:48:211617#else
drhcf014f62019-12-31 15:12:341618# define LOOKASIDE_SMALL 128
drhe6068022019-12-13 15:48:211619#endif
drh0225d812019-12-12 17:17:241620
drh633e6d52008-07-28 19:34:531621/*
drh80738d92016-02-15 00:34:161622** A hash table for built-in function definitions. (Application-defined
1623** functions use a regular table table from hash.h.)
drh70a8ca32008-08-21 18:49:271624**
1625** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
drha60c6302018-10-31 19:01:131626** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
1627** macro to compute a hash on the function name.
drh70a8ca32008-08-21 18:49:271628*/
drh80738d92016-02-15 00:34:161629#define SQLITE_FUNC_HASH_SZ 23
drh70a8ca32008-08-21 18:49:271630struct FuncDefHash {
drh80738d92016-02-15 00:34:161631 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
drh70a8ca32008-08-21 18:49:271632};
mistachkin8bee11a2018-10-29 17:53:231633#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
drh70a8ca32008-08-21 18:49:271634
drh32c6a482014-09-11 13:44:521635/*
1636** typedef for the authorization callback function.
1637*/
drhbc4df602024-10-28 17:27:151638typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
1639 const char*);
drh32c6a482014-09-11 13:44:521640
drh3d2a5292016-07-13 22:55:011641#ifndef SQLITE_OMIT_DEPRECATED
1642/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
1643** in the style of sqlite3_trace()
1644*/
drh04c67472018-12-04 14:33:021645#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */
1646#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */
drh3d2a5292016-07-13 22:55:011647#else
drh04c67472018-12-04 14:33:021648#define SQLITE_TRACE_LEGACY 0
1649#define SQLITE_TRACE_XPROFILE 0
drh3d2a5292016-07-13 22:55:011650#endif /* SQLITE_OMIT_DEPRECATED */
drh04c67472018-12-04 14:33:021651#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */
drh3d2a5292016-07-13 22:55:011652
drh099b3852021-03-10 16:35:371653/*
1654** Maximum number of sqlite3.aDb[] entries. This is the number of attached
1655** databases plus 2 for "main" and "temp".
1656*/
1657#define SQLITE_MAX_DB (SQLITE_MAX_ATTACHED+2)
drhd4530972014-09-09 14:47:531658
drh70a8ca32008-08-21 18:49:271659/*
drha2460e02010-01-14 00:39:261660** Each database connection is an instance of the following structure.
drh75897232000-05-29 14:26:001661*/
drh9bb575f2004-09-06 17:24:111662struct sqlite3 {
drh90f6a5b2007-08-15 13:04:541663 sqlite3_vfs *pVfs; /* OS Interface */
drha4510172012-02-02 15:50:171664 struct Vdbe *pVdbe; /* List of active virtual machines */
drh42a630b2020-03-05 16:13:241665 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
drha4510172012-02-02 15:50:171666 sqlite3_mutex *mutex; /* Connection mutex */
drh001bbcb2003-03-19 03:14:001667 Db *aDb; /* All backends */
drha4510172012-02-02 15:50:171668 int nDb; /* Number of backends currently in use */
drh8257aa82017-07-26 19:59:131669 u32 mDbFlags; /* flags recording internal state */
drhfd748c62018-10-30 16:25:351670 u64 flags; /* flags settable by pragmas. See below */
drha4510172012-02-02 15:50:171671 i64 lastRowid; /* ROWID of most recent insert (see above) */
drh9b4c59f2013-04-15 17:03:421672 i64 szMmap; /* Default mmap_size setting */
drhdc6b41e2017-08-17 02:26:351673 u32 nSchemaLock; /* Do not reset the schema when non-zero */
drh522c26f2011-05-07 14:40:291674 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
drhfcd35c72005-05-21 02:48:081675 int errCode; /* Most recent error code (SQLITE_*) */
drhf62641e2021-12-24 20:22:131676 int errByteOffset; /* Byte offset of error in SQL statement */
drh4ac285a2006-09-15 07:28:501677 int errMask; /* & result codes with this before returning */
drh1b9f2142016-03-17 16:01:231678 int iSysErrno; /* Errno value from last system error */
drhaf7b7652021-01-13 19:28:171679 u32 dbOptFlags; /* Flags to enable/disable optimizations */
drh9bd3cc42014-12-12 23:17:541680 u8 enc; /* Text encoding */
drhfcd35c72005-05-21 02:48:081681 u8 autoCommit; /* The auto-commit flag. */
drh90f5ecb2004-07-22 01:19:351682 u8 temp_store; /* 1: file 2: memory 0: default */
drh17435752007-08-16 04:30:381683 u8 mallocFailed; /* True if we have seen a malloc failure */
drh4a642b62016-02-05 01:55:271684 u8 bBenignMalloc; /* Do not require OOMs if true */
drh3b020132008-04-17 17:02:011685 u8 dfltLockMode; /* Default locking-mode for attached dbs */
drh98757152008-01-09 23:04:121686 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
drha7564662010-02-22 19:32:311687 u8 suppressErr; /* Do not issue error messages if true */
danb061d052011-04-25 18:49:571688 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
drha4510172012-02-02 15:50:171689 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
drh3d2a5292016-07-13 22:55:011690 u8 mTrace; /* zero or more SQLITE_TRACE flags */
drhb2c85592018-04-25 12:01:451691 u8 noSharedCache; /* True if no shared-cache backends */
drhbce04142017-02-23 00:58:361692 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
drh5f9de6e2021-08-07 23:16:521693 u8 eOpenState; /* Current condition of the connection */
danielk1977f653d782008-03-20 11:04:211694 int nextPagesize; /* Pagesize after VACUUM if >0 */
dan2c718872021-06-22 18:32:051695 i64 nChange; /* Value returned by sqlite3_changes() */
1696 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
drhcaa639f2008-03-20 00:32:201697 int aLimit[SQLITE_N_LIMIT]; /* Limits */
dan8930c2a2014-04-03 16:25:291698 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
danielk1977b28af712004-06-21 06:50:261699 struct sqlite3InitInfo { /* Information used during initialization */
drhabc38152020-07-22 13:38:041700 Pgno newTnum; /* Rootpage of table being initialized */
drha4510172012-02-02 15:50:171701 u8 iDb; /* Which db file is being initialized */
danielk1977b28af712004-06-21 06:50:261702 u8 busy; /* TRUE if currently initializing */
drhe6167352018-01-03 23:54:181703 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
1704 unsigned imposterTable : 1; /* Building an imposter table */
1705 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
drh2a6a72a2021-09-24 02:14:351706 const char **azInit; /* "type", "name", and "tbl_name" columns */
drh1d85d932004-02-14 23:05:521707 } init;
drh1713afb2013-06-28 01:24:571708 int nVdbeActive; /* Number of VDBEs currently running */
1709 int nVdbeRead; /* Number of active VDBEs that read or write */
1710 int nVdbeWrite; /* Number of active VDBEs that read and write */
1711 int nVdbeExec; /* Number of nested calls to VdbeExec() */
drh086723a2015-03-24 12:51:521712 int nVDestroy; /* Number of active OP_VDestroy operations */
drha4510172012-02-02 15:50:171713 int nExtension; /* Number of loaded extensions */
1714 void **aExtension; /* Array of shared library handles */
drh08b92082020-08-10 14:18:001715 union {
drh074a1312021-10-08 10:25:061716 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
1717 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
drh08b92082020-08-10 14:18:001718 } trace;
drh074a1312021-10-08 10:25:061719 void *pTraceArg; /* Argument to the trace function */
drh04c67472018-12-04 14:33:021720#ifndef SQLITE_OMIT_DEPRECATED
drh19e2d372005-08-29 23:00:031721 void (*xProfile)(void*,const char*,u64); /* Profiling function */
1722 void *pProfileArg; /* Argument to profile function */
drh04c67472018-12-04 14:33:021723#endif
mistachkinbfc9b3f2016-02-15 22:01:241724 void *pCommitArg; /* Argument to xCommitCallback() */
danielk197771fd80b2005-12-16 06:54:011725 int (*xCommitCallback)(void*); /* Invoked at every commit. */
mistachkinbfc9b3f2016-02-15 22:01:241726 void *pRollbackArg; /* Argument to xRollbackCallback() */
danielk197771fd80b2005-12-16 06:54:011727 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
danielk197794eb6a12005-12-15 15:22:081728 void *pUpdateArg;
1729 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
drh1bbfc672021-10-15 23:02:271730 void *pAutovacPagesArg; /* Client argument to autovac_pages */
1731 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
1732 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
drh1cf19752019-02-08 14:55:301733 Parse *pParse; /* Current parse */
drh9b1c62d2011-03-30 21:04:431734#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:071735 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
1736 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
1737 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
1738 );
1739 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
drh9b1c62d2011-03-30 21:04:431740#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan5cf53532010-05-01 16:40:201741#ifndef SQLITE_OMIT_WAL
drh7ed91f22010-04-29 22:34:071742 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
1743 void *pWalArg;
dan5cf53532010-05-01 16:40:201744#endif
drhfcd35c72005-05-21 02:48:081745 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
1746 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
1747 void *pCollNeededArg;
drhfcd35c72005-05-21 02:48:081748 sqlite3_value *pErr; /* Most recent error message */
drh881feaa2006-07-26 01:39:301749 union {
drh39001e72008-09-12 16:03:471750 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
drh881feaa2006-07-26 01:39:301751 double notUsed1; /* Spacer */
1752 } u1;
drh633e6d52008-07-28 19:34:531753 Lookaside lookaside; /* Lookaside malloc configuration */
drhed6c8672003-01-12 18:02:161754#ifndef SQLITE_OMIT_AUTHORIZATION
drh32c6a482014-09-11 13:44:521755 sqlite3_xauth xAuth; /* Access authorization function */
drhed6c8672003-01-12 18:02:161756 void *pAuthArg; /* 1st argument to the access auth function */
1757#endif
danielk1977348bb5d2003-10-18 09:37:261758#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1759 int (*xProgress)(void *); /* The progress callback */
1760 void *pProgressArg; /* Argument to the progress callback */
drh8f8c65f2013-07-10 18:14:291761 unsigned nProgressOps; /* Number of opcodes for progress callback */
danielk1977348bb5d2003-10-18 09:37:261762#endif
drhb9bb7c12006-06-11 23:41:551763#ifndef SQLITE_OMIT_VIRTUALTABLE
drha4510172012-02-02 15:50:171764 int nVTrans; /* Allocated size of aVTrans */
drhb9bb7c12006-06-11 23:41:551765 Hash aModule; /* populated by sqlite3_create_module() */
danb061d052011-04-25 18:49:571766 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
danielk1977595a5232009-07-24 17:58:531767 VTable **aVTrans; /* Virtual tables with open transactions */
drhefc88d02017-12-22 00:52:501768 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
danielk19776b456a22005-03-21 04:04:021769#endif
drh80738d92016-02-15 00:34:161770 Hash aFunc; /* Hash table of connection functions */
drhfcd35c72005-05-21 02:48:081771 Hash aCollSeq; /* All collating sequences */
1772 BusyHandler busyHandler; /* Busy callback */
1773 Db aDbStatic[2]; /* Static space for the 2 default backends */
danielk1977fd7f0452008-12-17 17:30:261774 Savepoint *pSavepoint; /* List of active savepoints */
drh49a76a82020-03-31 20:57:061775 int nAnalysisLimit; /* Number of index rows to ANALYZE */
drha4510172012-02-02 15:50:171776 int busyTimeout; /* Busy handler timeout, in msec */
dan43aad252025-01-27 11:50:031777#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
dan46288882025-01-30 15:26:161778 int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
dan2d878942025-02-10 20:46:141779 int setlkFlags; /* Flags passed to setlk_timeout() */
dan43aad252025-01-27 11:50:031780#endif
danielk1977fd7f0452008-12-17 17:30:261781 int nSavepoint; /* Number of non-transaction savepoints */
danielk1977bd434552009-03-18 10:33:001782 int nStatement; /* Number of nested statement-transactions */
dan1da40a32009-09-19 17:00:311783 i64 nDeferredCons; /* Net deferred constraints this transaction. */
drh648e2642013-07-11 15:03:321784 i64 nDeferredImmCons; /* Net deferred immediate constraints */
dand46def72010-07-24 11:28:281785 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
drh10deb352023-08-30 15:20:151786 DbClientData *pDbData; /* sqlite3_set_clientdata() content */
danielk1977404ca072009-03-16 13:19:361787#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
drhccb21132020-06-19 11:34:571788 /* The following variables are all protected by the STATIC_MAIN
mistachkinbfc9b3f2016-02-15 22:01:241789 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
drh65a73ba2009-04-07 22:06:571790 **
1791 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
1792 ** unlock so that it can proceed.
1793 **
1794 ** When X.pBlockingConnection==Y, that means that something that X tried
1795 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
1796 ** held by Y.
danielk1977404ca072009-03-16 13:19:361797 */
1798 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
1799 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
1800 void *pUnlockArg; /* Argument to xUnlockNotify */
1801 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
1802 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
1803#endif
drh75897232000-05-29 14:26:001804};
1805
drh03b808a2006-03-13 15:06:051806/*
1807** A macro to discover the encoding of a database.
1808*/
drh9bd3cc42014-12-12 23:17:541809#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
1810#define ENC(db) ((db)->enc)
danielk197714db2662006-01-09 16:12:041811
drh75897232000-05-29 14:26:001812/*
drhb945bcd2019-12-31 22:52:101813** A u64 constant where the lower 32 bits are all zeros. Only the
1814** upper 32 bits are included in the argument. Necessary because some
1815** C-compilers still do not accept LL integer literals.
1816*/
1817#define HI(X) ((u64)(X)<<32)
1818
1819/*
drh07096f62009-12-22 23:52:321820** Possible values for the sqlite3.flags.
drh49711602016-04-14 16:40:131821**
1822** Value constraints (enforced via assert()):
1823** SQLITE_FullFSync == PAGER_FULLFSYNC
1824** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
1825** SQLITE_CacheSpill == PAGER_CACHE_SPILL
drh75897232000-05-29 14:26:001826*/
drhccb21132020-06-19 11:34:571827#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_SCHEMA */
drh169dd922017-06-26 13:57:491828#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
drh6841b1c2016-02-03 19:20:151829#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
1830#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
1831#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
1832#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
drh40c39412013-08-16 20:42:201833#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
drhb77da372020-01-07 16:09:111834#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and
drh2928a152020-01-06 15:25:411835 ** vtabs in the schema definition */
drh67c82652020-01-04 20:58:411836#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
1837 /* result set is empty */
drh169dd922017-06-26 13:57:491838#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
dan06382de2023-02-28 20:04:011839#define SQLITE_StmtScanStatus 0x00000400 /* Enable stmt_scanstats() counters */
drh169dd922017-06-26 13:57:491840#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
1841#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
1842#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
1843#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
1844#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
1845#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
drh8257aa82017-07-26 19:59:131846#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
1847#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
1848#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
1849#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
1850#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
1851#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
drh36e31c62017-12-21 18:23:261852#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
1853#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
drh0314cf32018-04-28 01:27:091854#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
dan674b8942018-09-20 08:28:011855#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
drhfd748c62018-10-30 16:25:351856#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/
drha296cda2018-11-03 16:09:591857#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */
drhd0ff6012019-06-17 13:56:111858#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
1859#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
drh11d88e62019-08-15 21:27:201860#define SQLITE_EnableView 0x80000000 /* Enable the use of views */
drhb945bcd2019-12-31 22:52:101861#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */
1862 /* DELETE, or UPDATE and return */
1863 /* the count using a callback. */
drh46c425b2021-11-10 10:59:101864#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */
dan45163fc2023-02-28 19:39:591865#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
dan17c34082023-10-20 17:06:391866#define SQLITE_FkNoAction HI(0x00008) /* Treat all FK as NO ACTION */
drhc850c2b2025-01-22 19:37:471867#define SQLITE_AttachCreate HI(0x00010) /* ATTACH allowed to create new dbs */
1868#define SQLITE_AttachWrite HI(0x00020) /* ATTACH allowed to open for write */
drhe16b3452025-01-31 01:34:191869#define SQLITE_Comments HI(0x00040) /* Enable SQL comments */
drh36e31c62017-12-21 18:23:261870
drh169dd922017-06-26 13:57:491871/* Flags used only if debugging */
1872#ifdef SQLITE_DEBUG
drh11d88e62019-08-15 21:27:201873#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */
1874#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */
1875#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */
1876#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
1877#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
1878#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */
drh169dd922017-06-26 13:57:491879#endif
drhb1eaa712013-07-11 15:22:311880
drh8257aa82017-07-26 19:59:131881/*
1882** Allowed values for sqlite3.mDbFlags
1883*/
1884#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
1885#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
1886#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
drh4e61e882019-04-04 14:00:231887#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */
1888#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */
drh171c50e2020-01-01 15:43:301889#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */
dan0ea2d422020-03-05 18:04:091890#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */
drh07096f62009-12-22 23:52:321891
1892/*
drh7e5418e2012-09-27 15:05:541893** Bits of the sqlite3.dbOptFlags field that are used by the
1894** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
1895** selectively disable various optimizations.
drh07096f62009-12-22 23:52:321896*/
drhaf7b7652021-01-13 19:28:171897#define SQLITE_QueryFlattener 0x00000001 /* Query flattening */
1898#define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */
1899#define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */
1900#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */
1901#define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */
1902#define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */
1903#define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */
1904#define SQLITE_Transitive 0x00000080 /* Transitive constraints */
1905#define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */
1906#define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
1907#define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */
1908#define SQLITE_Stat4 0x00000800 /* Use STAT4 data */
1909 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
drh05c6d132024-04-07 10:27:181910#define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */
drhaf7b7652021-01-13 19:28:171911#define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */
1912#define SQLITE_SkipScan 0x00004000 /* Skip-scans */
1913#define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */
1914#define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */
drh3074faa2021-06-02 19:28:071915#define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
drhbb301232021-07-15 19:29:431916#define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
drhef8344c2021-07-16 22:43:001917 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
drh2db144c2021-12-01 16:31:021918#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */
drh6ae49e62021-12-05 20:19:471919#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
drh38cebe02021-12-30 00:37:111920#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
drhda4c7cc2022-04-07 18:17:561921#define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */
drh95fe38f2022-04-25 14:59:591922#define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */
drhc35f02d2022-05-02 15:31:061923 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
drhc046f6d2022-10-20 16:30:051924#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */
drhad9ff1d2022-12-06 15:24:051925#define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
drh7defd202023-02-16 18:04:491926#define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */
drh1f097a22023-07-31 17:39:361927#define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
drh235b5d02024-08-15 23:38:521928#define SQLITE_OrderBySubq 0x10000000 /* ORDER BY in subquery helps outer */
drh36407852025-01-19 19:14:211929#define SQLITE_StarQuery 0x20000000 /* Heurists for star queries */
drhaa54d7a2025-07-02 20:46:021930#define SQLITE_ExistsToJoin 0x40000000 /* The EXISTS-to-JOIN optimization */
drhaf7b7652021-01-13 19:28:171931#define SQLITE_AllOpts 0xffffffff /* All optimizations */
drh7e5418e2012-09-27 15:05:541932
1933/*
1934** Macros for testing whether or not optimizations are enabled or disabled.
1935*/
drh7e5418e2012-09-27 15:05:541936#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
1937#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
danielk197734c68fb2007-03-14 15:37:041938
drh58b95762000-06-02 01:17:371939/*
drhd9f158e2013-11-21 20:48:421940** Return true if it OK to factor constant expressions into the initialization
1941** code. The argument is a Parse object for the code generator.
1942*/
drhaceb31b2014-02-08 01:40:271943#define ConstFactorOk(P) ((P)->okConstFactor)
drhd9f158e2013-11-21 20:48:421944
drhc8069172021-08-09 17:36:221945/* Possible values for the sqlite3.eOpenState field.
1946** The numbers are randomly selected such that a minimum of three bits must
1947** change to convert any number to another or to zero
drh247be432002-05-10 05:44:551948*/
mistachkinbdf15bb2021-08-09 18:13:381949#define SQLITE_STATE_OPEN 0x76 /* Database is open */
1950#define SQLITE_STATE_CLOSED 0xce /* Database is closed */
1951#define SQLITE_STATE_SICK 0xba /* Error and awaiting close */
1952#define SQLITE_STATE_BUSY 0x6d /* Database currently in use */
1953#define SQLITE_STATE_ERROR 0xd5 /* An SQLITE_MISUSE error occurred */
1954#define SQLITE_STATE_ZOMBIE 0xa7 /* Close with last statement close */
drh247be432002-05-10 05:44:551955
1956/*
drh0bce8352002-02-28 00:41:101957** Each SQL function is defined by an instance of the following
drh80738d92016-02-15 00:34:161958** structure. For global built-in functions (ex: substr(), max(), count())
1959** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
1960** For per-connection application-defined functions, a pointer to this
1961** structure is held in the db->aHash hash table.
1962**
1963** The u.pHash field is used by the global built-ins. The u.pDestructor
1964** field is used by per-connection app-def functions.
drh28037572000-08-02 13:47:411965*/
drh0bce8352002-02-28 00:41:101966struct FuncDef {
drh35d302c2024-12-12 15:11:271967 i16 nArg; /* Number of arguments. -1 means unlimited */
dandfa552f2018-06-02 21:04:281968 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
drhf9b596e2004-05-26 16:54:421969 void *pUserData; /* User data parameter */
1970 FuncDef *pNext; /* Next function with same name */
drh2d801512016-01-14 22:19:581971 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
1972 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
dan86fb6e12018-05-16 20:58:071973 void (*xValue)(sqlite3_context*); /* Current agg value */
1974 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
drh6ad224e2016-02-24 19:57:111975 const char *zName; /* SQL name of the function. */
drh80738d92016-02-15 00:34:161976 union {
1977 FuncDef *pHash; /* Next with a different name but the same hash */
1978 FuncDestructor *pDestructor; /* Reference counted destructor function */
drhf9751072021-10-07 13:40:291979 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
dand2199f02010-08-27 17:48:521980};
1981
1982/*
1983** This structure encapsulates a user-function destructor callback (as
1984** configured using create_function_v2()) and a reference counter. When
1985** create_function_v2() is called to create a function with a destructor,
mistachkinbfc9b3f2016-02-15 22:01:241986** a single object of this type is allocated. FuncDestructor.nRef is set to
dand2199f02010-08-27 17:48:521987** the number of FuncDef objects created (either 1 or 3, depending on whether
1988** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
1989** member of each of the new FuncDef objects is set to point to the allocated
1990** FuncDestructor.
1991**
1992** Thereafter, when one of the FuncDef objects is deleted, the reference
1993** count on this object is decremented. When it reaches 0, the destructor
1994** is invoked and the FuncDestructor structure freed.
1995*/
1996struct FuncDestructor {
1997 int nRef;
1998 void (*xDestroy)(void *);
1999 void *pUserData;
drh8e0a2f92002-02-23 23:45:452000};
drh28037572000-08-02 13:47:412001
2002/*
drha748fdc2012-03-28 01:34:472003** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
drh7977fa32015-11-20 13:17:292004** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
2005** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
drha748fdc2012-03-28 01:34:472006** are assert() statements in the code to verify this.
drh49711602016-04-14 16:40:132007**
2008** Value constraints (enforced via assert()):
drhbb301232021-07-15 19:29:432009** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
2010** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
2011** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
2012** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
drh077efc22023-06-22 21:19:372013** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG
drhbb301232021-07-15 19:29:432014** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
2015** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
drh67918912023-01-09 12:01:302016** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
drh49711602016-04-14 16:40:132017** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
drh67918912023-01-09 12:01:302018**
2019** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the
2020** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is
larrybrbc917382023-06-07 08:40:312021** used internally and if set means that the function has side effects.
drh67918912023-01-09 12:01:302022** SQLITE_INNOCUOUS is used by application code and means "not unsafe".
2023** See multiple instances of tag-20230109-1.
drh7d10d5a2008-08-20 16:35:102024*/
drh1d85e402015-08-31 17:34:412025#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
2026#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
2027#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
2028#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
2029#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
2030#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
2031#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
drh077efc22023-06-22 21:19:372032#define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
drh1d85e402015-08-31 17:34:412033#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
drhffe421c2020-05-13 17:26:382034/* 0x0200 -- available for reuse */
drh1d85e402015-08-31 17:34:412035#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
2036#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
2037#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
drha7f910b2015-09-01 13:17:172038#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
2039 ** single query - might change over time */
drh25c42962020-01-01 13:55:082040#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
drh17a32952023-11-07 19:03:132041#define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
drheea8eb62018-11-26 18:09:152042#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
drheea8eb62018-11-26 18:09:152043#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
drh42d2fce2019-08-15 20:04:092044#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
drh194b8d52023-11-09 12:08:162045/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
drh4be621e2020-01-03 21:57:532046#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
drhc4ad8492020-01-03 20:57:382047#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
drhf9751072021-10-07 13:40:292048#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
drh243f2ec2023-11-08 21:38:302049/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
drhbb301232021-07-15 19:29:432050#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
drhc4ad8492020-01-03 20:57:382051
drh25c42962020-01-01 13:55:082052/* Identifier numbers for each in-line function */
drh171c50e2020-01-01 15:43:302053#define INLINEFUNC_coalesce 0
2054#define INLINEFUNC_implies_nonnull_row 1
2055#define INLINEFUNC_expr_implies_expr 2
larrybrbc917382023-06-07 08:40:312056#define INLINEFUNC_expr_compare 3
drh171c50e2020-01-01 15:43:302057#define INLINEFUNC_affinity 4
drh3c0e6062020-05-13 18:03:342058#define INLINEFUNC_iif 5
drh645682a2022-06-01 11:05:592059#define INLINEFUNC_sqlite_offset 6
drh171c50e2020-01-01 15:43:302060#define INLINEFUNC_unlikely 99 /* Default case */
drh7d10d5a2008-08-20 16:35:102061
2062/*
drh777c5382008-08-21 20:21:342063** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
2064** used to create the initializers for the FuncDef structures.
2065**
2066** FUNCTION(zName, nArg, iArg, bNC, xFunc)
mistachkinbfc9b3f2016-02-15 22:01:242067** Used to create a scalar function definition of a function zName
drh777c5382008-08-21 20:21:342068** implemented by C function xFunc that accepts nArg arguments. The
2069** value passed as iArg is cast to a (void*) and made available
mistachkinbfc9b3f2016-02-15 22:01:242070** as the user-data (sqlite3_user_data()) for the function. If
drhf7bca572009-05-30 14:16:312071** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
drh777c5382008-08-21 20:21:342072**
drhb1fba282013-11-21 14:33:482073** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
2074** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
2075**
drh64de2a52019-12-31 18:39:232076** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
2077** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
2078** adds the SQLITE_DIRECTONLY flag.
2079**
drh25c42962020-01-01 13:55:082080** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
2081** zName is the name of a function that is implemented by in-line
2082** byte code rather than by the usual callbacks. The iFuncId
2083** parameter determines the function id. The mFlags parameter is
2084** optional SQLITE_FUNC_ flags for this function.
2085**
2086** TEST_FUNC(zName, nArg, iFuncId, mFlags)
2087** zName is the name of a test-only function implemented by in-line
2088** byte code rather than by the usual callbacks. The iFuncId
2089** parameter determines the function id. The mFlags parameter is
2090** optional SQLITE_FUNC_ flags for this function.
2091**
drh1d85e402015-08-31 17:34:412092** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
2093** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
drha7f910b2015-09-01 13:17:172094** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
drh03bf26d2015-08-31 21:16:362095** and functions like sqlite_version() that can change, but not during
drh3e34eab2017-07-19 19:48:402096** a single query. The iArg is ignored. The user-data is always set
2097** to a NULL pointer. The bNC parameter is not used.
2098**
drhf6e904b2020-12-07 17:15:322099** MFUNCTION(zName, nArg, xPtr, xFunc)
2100** For math-library functions. xPtr is an arbitrary pointer.
2101**
drh3e34eab2017-07-19 19:48:402102** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
2103** Used for "pure" date/time functions, this macro is like DFUNCTION
2104** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
larrybrbc917382023-06-07 08:40:312105** ignored and the user-data for these functions is set to an
drh3e34eab2017-07-19 19:48:402106** arbitrary non-NULL pointer. The bNC parameter is not used.
drh1d85e402015-08-31 17:34:412107**
drh777c5382008-08-21 20:21:342108** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
2109** Used to create an aggregate function definition implemented by
2110** the C functions xStep and xFinal. The first four parameters
2111** are interpreted in the same way as the first 4 parameters to
2112** FUNCTION().
2113**
drh9dbf96b2022-01-06 01:40:092114** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
dan86fb6e12018-05-16 20:58:072115** Used to create an aggregate function definition implemented by
2116** the C functions xStep and xFinal. The first four parameters
2117** are interpreted in the same way as the first 4 parameters to
2118** FUNCTION().
2119**
drh777c5382008-08-21 20:21:342120** LIKEFUNC(zName, nArg, pArg, flags)
mistachkinbfc9b3f2016-02-15 22:01:242121** Used to create a scalar function definition of a function zName
2122** that accepts nArg arguments and is implemented by a call to C
drh777c5382008-08-21 20:21:342123** function likeFunc. Argument pArg is cast to a (void *) and made
2124** available as the function user-data (sqlite3_user_data()). The
2125** FuncDef.flags variable is set to the value passed as the flags
2126** parameter.
2127*/
2128#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292129 {nArg, SQLITE_FUNC_BUILTIN|\
2130 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
dan86fb6e12018-05-16 20:58:072131 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
drhb1fba282013-11-21 14:33:482132#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292133 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
dan86fb6e12018-05-16 20:58:072134 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
drh64de2a52019-12-31 18:39:232135#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292136 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \
drh64de2a52019-12-31 18:39:232137 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
drhf6e904b2020-12-07 17:15:322138#define MFUNCTION(zName, nArg, xPtr, xFunc) \
drhf9751072021-10-07 13:40:292139 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
drhf6e904b2020-12-07 17:15:322140 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
drhe8d4fd52023-11-10 18:59:232141#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, bJsonB, iArg, xFunc) \
drhb4943662023-11-08 16:37:122142 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
drh243f2ec2023-11-08 21:38:302143 SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
drh194b8d52023-11-09 12:08:162144 ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
drha4cf38c2023-11-08 17:11:132145 SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB)),0,xFunc,0, 0, 0, #zName, {0} }
drh25c42962020-01-01 13:55:082146#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
drhf9751072021-10-07 13:40:292147 {nArg, SQLITE_FUNC_BUILTIN|\
2148 SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
drh25c42962020-01-01 13:55:082149 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
2150#define TEST_FUNC(zName, nArg, iArg, mFlags) \
drhf9751072021-10-07 13:40:292151 {nArg, SQLITE_FUNC_BUILTIN|\
2152 SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \
drh4be621e2020-01-03 21:57:532153 SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
drh25c42962020-01-01 13:55:082154 SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} }
drh1d85e402015-08-31 17:34:412155#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292156 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
dan86fb6e12018-05-16 20:58:072157 0, 0, xFunc, 0, 0, 0, #zName, {0} }
drh3e34eab2017-07-19 19:48:402158#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292159 {nArg, SQLITE_FUNC_BUILTIN|\
2160 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
dan86fb6e12018-05-16 20:58:072161 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
drha748fdc2012-03-28 01:34:472162#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
drhf9751072021-10-07 13:40:292163 {nArg, SQLITE_FUNC_BUILTIN|\
2164 SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
dan86fb6e12018-05-16 20:58:072165 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
drh21717ed2008-10-13 15:35:082166#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
drhf9751072021-10-07 13:40:292167 {nArg, SQLITE_FUNC_BUILTIN|\
2168 SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
drhf8addcf2025-07-14 09:41:592169 pArg, 0, xFunc, 0, 0, 0, #zName, {0} }
drh777c5382008-08-21 20:21:342170#define LIKEFUNC(zName, nArg, arg, flags) \
drhf9751072021-10-07 13:40:292171 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
dan86fb6e12018-05-16 20:58:072172 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
dan6fb2b542018-06-19 17:13:112173#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
drhf9751072021-10-07 13:40:292174 {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
dan03854d22018-06-08 11:45:282175 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
drheea8eb62018-11-26 18:09:152176#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
drhf9751072021-10-07 13:40:292177 {nArg, SQLITE_FUNC_BUILTIN|\
2178 SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
drheea8eb62018-11-26 18:09:152179 0, 0, xFunc, 0, 0, 0, #zName, {0} }
2180
drh777c5382008-08-21 20:21:342181
danielk1977fd7f0452008-12-17 17:30:262182/*
2183** All current savepoints are stored in a linked list starting at
2184** sqlite3.pSavepoint. The first element in the list is the most recently
2185** opened savepoint. Savepoints are added to the list by the vdbe
2186** OP_Savepoint instruction.
2187*/
2188struct Savepoint {
2189 char *zName; /* Savepoint name (nul-terminated) */
drhfcb9f7a2009-10-13 19:19:232190 i64 nDeferredCons; /* Number of deferred fk violations */
drh648e2642013-07-11 15:03:322191 i64 nDeferredImmCons; /* Number of deferred imm fk. */
danielk1977fd7f0452008-12-17 17:30:262192 Savepoint *pNext; /* Parent savepoint (if any) */
2193};
2194
2195/*
2196** The following are used as the second parameter to sqlite3Savepoint(),
2197** and as the P1 argument to the OP_Savepoint instruction.
2198*/
2199#define SAVEPOINT_BEGIN 0
2200#define SAVEPOINT_RELEASE 1
2201#define SAVEPOINT_ROLLBACK 2
2202
drh777c5382008-08-21 20:21:342203
2204/*
danielk1977d1ab1ba2006-06-15 04:28:132205** Each SQLite module (virtual table definition) is defined by an
2206** instance of the following structure, stored in the sqlite3.aModule
2207** hash table.
2208*/
2209struct Module {
2210 const sqlite3_module *pModule; /* Callback pointers */
2211 const char *zName; /* Name passed to create_module() */
drhcc5979d2019-08-16 22:58:292212 int nRefModule; /* Number of pointers to this object */
danielk1977d1ab1ba2006-06-15 04:28:132213 void *pAux; /* pAux passed to create_module() */
danielk1977832a58a2007-06-22 15:21:152214 void (*xDestroy)(void *); /* Module destructor function */
drh51be3872015-08-19 02:32:252215 Table *pEpoTab; /* Eponymous table for this module */
danielk1977d1ab1ba2006-06-15 04:28:132216};
2217
2218/*
drhb9bcf7c2019-10-19 13:29:102219** Information about each column of an SQL table is held in an instance
2220** of the Column structure, in the Table.aCol[] array.
2221**
2222** Definitions:
2223**
2224** "table column index" This is the index of the column in the
2225** Table.aCol[] array, and also the index of
2226** the column in the original CREATE TABLE stmt.
2227**
2228** "storage column index" This is the index of the column in the
2229** record BLOB generated by the OP_MakeRecord
2230** opcode. The storage column index is less than
2231** or equal to the table column index. It is
2232** equal if and only if there are no VIRTUAL
2233** columns to the left.
drh65b40092021-08-05 15:27:192234**
2235** Notes on zCnName:
2236** The zCnName field stores the name of the column, the datatype of the
2237** column, and the collating sequence for the column, in that order, all in
2238** a single allocation. Each string is 0x00 terminated. The datatype
2239** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
2240** collating sequence name is only included if the COLFLAG_HASCOLL bit is
2241** set.
drh7020f652000-06-03 18:06:522242*/
2243struct Column {
drh15482bc2021-08-06 15:26:012244 char *zCnName; /* Name of this column */
2245 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
drh72532f52021-08-18 19:22:272246 unsigned eCType :4; /* One of the standard types */
drh15482bc2021-08-06 15:26:012247 char affinity; /* One of the SQLITE_AFF_... values */
2248 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
2249 u8 hName; /* Column name hash for faster lookup */
2250 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
2251 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
drh7020f652000-06-03 18:06:522252};
2253
drhb70f2ea2021-08-18 12:05:222254/* Allowed values for Column.eCType.
drhc2df4d62021-07-30 23:30:302255**
2256** Values must match entries in the global constant arrays
2257** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
2258** than the offset into these arrays for the corresponding name.
2259** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
2260*/
2261#define COLTYPE_CUSTOM 0 /* Type appended to zName */
drhb9fd0102021-08-23 10:28:022262#define COLTYPE_ANY 1
2263#define COLTYPE_BLOB 2
2264#define COLTYPE_INT 3
2265#define COLTYPE_INTEGER 4
2266#define COLTYPE_REAL 5
2267#define COLTYPE_TEXT 6
2268#define SQLITE_N_STDTYPE 6 /* Number of standard types */
drhc2df4d62021-07-30 23:30:302269
drh6f6e60d2021-02-18 15:45:342270/* Allowed values for Column.colFlags.
2271**
2272** Constraints:
2273** TF_HasVirtual == COLFLAG_VIRTUAL
2274** TF_HasStored == COLFLAG_STORED
2275** TF_HasHidden == COLFLAG_HIDDEN
drha371ace2012-09-13 14:22:472276*/
drh81f7b372019-10-16 12:18:592277#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
2278#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
2279#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
2280#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
dan2e3a5a82018-04-16 21:12:422281#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
drh81f7b372019-10-16 12:18:592282#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
2283#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */
drh12bf7122019-11-09 15:31:342284#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */
2285#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */
drh65b40092021-08-05 15:27:192286#define COLFLAG_HASCOLL 0x0200 /* Has collating sequence name in zCnName */
drhab843a52022-04-23 07:29:342287#define COLFLAG_NOEXPAND 0x0400 /* Omit this column when expanding "*" */
drhc27ea2a2019-10-16 20:05:562288#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */
drh7e508f12019-10-16 19:31:462289#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
drha371ace2012-09-13 14:22:472290
drh7020f652000-06-03 18:06:522291/*
drha9fd84b2004-05-18 23:21:352292** A "Collating Sequence" is defined by an instance of the following
danielk19770202b292004-06-09 09:55:162293** structure. Conceptually, a collating sequence consists of a name and
2294** a comparison routine that defines the order of that sequence.
drha9fd84b2004-05-18 23:21:352295**
drhe6f1e762012-12-06 01:03:152296** If CollSeq.xCmp is NULL, it means that the
danielk19770202b292004-06-09 09:55:162297** collating sequence is undefined. Indices built on an undefined
2298** collating sequence may not be read or written.
drha9fd84b2004-05-18 23:21:352299*/
2300struct CollSeq {
danielk1977a9808b32007-05-07 09:32:452301 char *zName; /* Name of the collating sequence, UTF-8 encoded */
2302 u8 enc; /* Text encoding handled by xCmp() */
danielk1977a9808b32007-05-07 09:32:452303 void *pUser; /* First argument to xCmp() */
danielk19770202b292004-06-09 09:55:162304 int (*xCmp)(void*,int, const void*, int, const void*);
danielk1977a9808b32007-05-07 09:32:452305 void (*xDel)(void*); /* Destructor for pUser */
drha9fd84b2004-05-18 23:21:352306};
2307
2308/*
drhd3d39e92004-05-20 22:16:292309** A sort order can be either ASC or DESC.
drh8e2ca022002-06-17 17:07:192310*/
drh8e2ca022002-06-17 17:07:192311#define SQLITE_SO_ASC 0 /* Sort in ascending order */
drhd3d39e92004-05-20 22:16:292312#define SQLITE_SO_DESC 1 /* Sort in ascending order */
drhbc622bc2015-08-24 15:39:422313#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
drh8e2ca022002-06-17 17:07:192314
2315/*
danielk1977a37cdde2004-05-16 11:15:362316** Column affinity types.
drh8a512562005-11-14 22:29:052317**
2318** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
2319** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
mistachkinbfc9b3f2016-02-15 22:01:242320** the speed a little by numbering the values consecutively.
drh8a512562005-11-14 22:29:052321**
drh4583c372014-09-19 20:13:252322** But rather than start with 0 or 1, we begin with 'A'. That way,
drh8a512562005-11-14 22:29:052323** when multiple affinity types are concatenated into a string and
drh66a51672008-01-03 00:01:232324** used as the P4 operand, they will be more readable.
drh8a512562005-11-14 22:29:052325**
2326** Note also that the numeric types are grouped together so that testing
drh05883a32015-06-02 15:32:082327** for a numeric type is a single comparison. And the BLOB type is first.
danielk1977a37cdde2004-05-16 11:15:362328*/
drh96fb16e2019-08-06 14:37:242329#define SQLITE_AFF_NONE 0x40 /* '@' */
2330#define SQLITE_AFF_BLOB 0x41 /* 'A' */
2331#define SQLITE_AFF_TEXT 0x42 /* 'B' */
2332#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */
2333#define SQLITE_AFF_INTEGER 0x44 /* 'D' */
2334#define SQLITE_AFF_REAL 0x45 /* 'E' */
drh00d6b272022-12-15 20:03:082335#define SQLITE_AFF_FLEXNUM 0x46 /* 'F' */
drh342ef632025-06-02 18:34:172336#define SQLITE_AFF_DEFER 0x58 /* 'X' - defer computation until later */
danielk1977a37cdde2004-05-16 11:15:362337
drh8a512562005-11-14 22:29:052338#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
danielk1977a37cdde2004-05-16 11:15:362339
2340/*
drh35573352008-01-08 23:54:252341** The SQLITE_AFF_MASK values masks off the significant bits of an
mistachkinbfc9b3f2016-02-15 22:01:242342** affinity value.
drh35573352008-01-08 23:54:252343*/
drh4583c372014-09-19 20:13:252344#define SQLITE_AFF_MASK 0x47
drh35573352008-01-08 23:54:252345
2346/*
2347** Additional bit values that can be ORed with an affinity without
2348** changing the affinity.
drh3d77dee2014-02-19 14:20:492349**
2350** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
2351** It causes an assert() to fire if either operand to a comparison
2352** operator is NULL. It is added to certain comparison operators to
2353** prove that the operands are always NOT NULL.
drh35573352008-01-08 23:54:252354*/
drh4583c372014-09-19 20:13:252355#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
drh6a2fe092009-09-23 02:29:362356#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
drh4583c372014-09-19 20:13:252357#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
drh35573352008-01-08 23:54:252358
2359/*
danielk1977595a5232009-07-24 17:58:532360** An object of this type is created for each virtual table present in
mistachkinbfc9b3f2016-02-15 22:01:242361** the database schema.
danielk1977595a5232009-07-24 17:58:532362**
2363** If the database schema is shared, then there is one instance of this
2364** structure for each database connection (sqlite3*) that uses the shared
2365** schema. This is because each database connection requires its own unique
mistachkinbfc9b3f2016-02-15 22:01:242366** instance of the sqlite3_vtab* handle used to access the virtual table
2367** implementation. sqlite3_vtab* handles can not be shared between
2368** database connections, even when the rest of the in-memory database
danielk1977595a5232009-07-24 17:58:532369** schema is shared, as the implementation often stores the database
2370** connection handle passed to it via the xConnect() or xCreate() method
2371** during initialization internally. This database connection handle may
mistachkinbfc9b3f2016-02-15 22:01:242372** then be used by the virtual table implementation to access real tables
2373** within the database. So that they appear as part of the callers
2374** transaction, these accesses need to be made via the same database
danielk1977595a5232009-07-24 17:58:532375** connection as that used to execute SQL operations on the virtual table.
2376**
2377** All VTable objects that correspond to a single table in a shared
2378** database schema are initially stored in a linked-list pointed to by
2379** the Table.pVTable member variable of the corresponding Table object.
2380** When an sqlite3_prepare() operation is required to access the virtual
2381** table, it searches the list for the VTable that corresponds to the
2382** database connection doing the preparing so as to use the correct
2383** sqlite3_vtab* handle in the compiled query.
2384**
2385** When an in-memory Table object is deleted (for example when the
mistachkinbfc9b3f2016-02-15 22:01:242386** schema is being reloaded for some reason), the VTable objects are not
2387** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
danielk1977595a5232009-07-24 17:58:532388** immediately. Instead, they are moved from the Table.pVTable list to
2389** another linked list headed by the sqlite3.pDisconnect member of the
mistachkinbfc9b3f2016-02-15 22:01:242390** corresponding sqlite3 structure. They are then deleted/xDisconnected
danielk1977595a5232009-07-24 17:58:532391** next time a statement is prepared using said sqlite3*. This is done
2392** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
2393** Refer to comments above function sqlite3VtabUnlockList() for an
2394** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
2395** list without holding the corresponding sqlite3.mutex mutex.
2396**
mistachkinbfc9b3f2016-02-15 22:01:242397** The memory for objects of this type is always allocated by
2398** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
danielk1977595a5232009-07-24 17:58:532399** the first argument.
2400*/
2401struct VTable {
2402 sqlite3 *db; /* Database connection associated with this table */
2403 Module *pMod; /* Pointer to module implementation */
2404 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
2405 int nRef; /* Number of pointers to this structure */
danb061d052011-04-25 18:49:572406 u8 bConstraint; /* True if constraints are supported */
drh0669d6e2023-04-03 15:01:372407 u8 bAllSchemas; /* True if might use any attached schema */
drh2928a152020-01-06 15:25:412408 u8 eVtabRisk; /* Riskiness of allowing hacker access */
drhaddd8f82011-05-25 15:54:092409 int iSavepoint; /* Depth of the SAVEPOINT stack */
danielk1977595a5232009-07-24 17:58:532410 VTable *pNext; /* Next in linked list (see above) */
2411};
2412
drh2928a152020-01-06 15:25:412413/* Allowed values for VTable.eVtabRisk
2414*/
2415#define SQLITE_VTABRISK_Low 0
2416#define SQLITE_VTABRISK_Normal 1
2417#define SQLITE_VTABRISK_High 2
2418
danielk1977595a5232009-07-24 17:58:532419/*
drhf38524d2021-08-02 16:41:572420** The schema for each SQL table, virtual table, and view is represented
2421** in memory by an instance of the following structure.
drh75897232000-05-29 14:26:002422*/
2423struct Table {
drh7d10d5a2008-08-20 16:35:102424 char *zName; /* Name of the table or view */
drh7d10d5a2008-08-20 16:35:102425 Column *aCol; /* Information about each column */
2426 Index *pIndex; /* List of SQL indexes on this table. */
drh7d10d5a2008-08-20 16:35:102427 char *zColAff; /* String defining the affinity of each column */
drh2938f922012-03-07 19:13:292428 ExprList *pCheck; /* All CHECK constraints */
drh8981b902015-08-24 17:42:492429 /* ... also used as column name list in a VIEW */
drhabc38152020-07-22 13:38:042430 Pgno tnum; /* Root BTree page for this table */
drh79df7782016-12-14 14:07:352431 u32 nTabRef; /* Number of pointers to this Table */
drh6f271a42017-02-16 15:57:302432 u32 tabFlags; /* Mask of TF_* values */
drh5789d1a2015-05-01 15:25:512433 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
drhd815f172012-09-13 14:42:432434 i16 nCol; /* Number of columns in this table */
drh0b0b3a92019-10-17 18:35:572435 i16 nNVCol; /* Number of columns that are not VIRTUAL */
drh5789d1a2015-05-01 15:25:512436 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
drhbf539c42013-10-05 18:16:022437 LogEst szTabRow; /* Estimated size of each table row in bytes */
drhdbd94862014-07-23 23:57:422438#ifdef SQLITE_ENABLE_COSTMULT
2439 LogEst costMult; /* Cost multiplier for using this table */
2440#endif
drhd815f172012-09-13 14:42:432441 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
drhf38524d2021-08-02 16:41:572442 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
2443 union {
2444 struct { /* Used by ordinary tables: */
2445 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
2446 FKey *pFKey; /* Linked list of all foreign keys in this table */
2447 ExprList *pDfltList; /* DEFAULT clauses on various columns.
2448 ** Or the AS clause for generated columns. */
2449 } tab;
2450 struct { /* Used by views: */
2451 Select *pSelect; /* View definition */
2452 } view;
2453 struct { /* Used by virtual tables only: */
2454 int nArg; /* Number of arguments to the module */
2455 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
2456 VTable *p; /* List of VTable objects. */
2457 } vtab;
2458 } u;
2459 Trigger *pTrigger; /* List of triggers on this object */
drh7d10d5a2008-08-20 16:35:102460 Schema *pSchema; /* Schema that contains this table */
drh66172ce2025-02-08 16:16:082461 u8 aHx[16]; /* Column aHt[K%sizeof(aHt)] might have hash K */
drh75897232000-05-29 14:26:002462};
2463
2464/*
dan8ce71842014-01-14 20:14:092465** Allowed values for Table.tabFlags.
drha21f78b2015-04-19 18:32:432466**
drh80090f92015-11-19 17:55:112467** TF_OOOHidden applies to tables or view that have hidden columns that are
drha21f78b2015-04-19 18:32:432468** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
2469** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
2470** the TF_OOOHidden attribute would apply in this case. Such tables require
drhc1431142019-10-17 17:54:052471** special handling during INSERT processing. The "OOO" means "Out Of Order".
2472**
2473** Constraints:
2474**
drh6f6e60d2021-02-18 15:45:342475** TF_HasVirtual == COLFLAG_VIRTUAL
2476** TF_HasStored == COLFLAG_STORED
2477** TF_HasHidden == COLFLAG_HIDDEN
drh7d10d5a2008-08-20 16:35:102478*/
drhf38524d2021-08-02 16:41:572479#define TF_Readonly 0x00000001 /* Read-only system table */
2480#define TF_HasHidden 0x00000002 /* Has one or more hidden columns */
2481#define TF_HasPrimaryKey 0x00000004 /* Table has a primary key */
2482#define TF_Autoincrement 0x00000008 /* Integer primary key is autoincrement */
2483#define TF_HasStat1 0x00000010 /* nRowLogEst set from sqlite_stat1 */
2484#define TF_HasVirtual 0x00000020 /* Has one or more VIRTUAL columns */
2485#define TF_HasStored 0x00000040 /* Has one or more STORED columns */
2486#define TF_HasGenerated 0x00000060 /* Combo: HasVirtual + HasStored */
2487#define TF_WithoutRowid 0x00000080 /* No rowid. PRIMARY KEY is the key */
drh9d00aba2024-02-16 12:57:042488#define TF_MaybeReanalyze 0x00000100 /* Maybe run ANALYZE on this table */
drhf38524d2021-08-02 16:41:572489#define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
2490#define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
2491#define TF_HasNotNull 0x00000800 /* Contains NOT NULL constraints */
2492#define TF_Shadow 0x00001000 /* True for a shadow table */
2493#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */
2494#define TF_Ephemeral 0x00004000 /* An ephemeral table */
2495#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
drh44183f82021-08-18 13:13:582496#define TF_Strict 0x00010000 /* STRICT mode */
drhf38524d2021-08-02 16:41:572497
2498/*
2499** Allowed values for Table.eTabType
2500*/
2501#define TABTYP_NORM 0 /* Ordinary table */
2502#define TABTYP_VTAB 1 /* Virtual table */
2503#define TABTYP_VIEW 2 /* A view */
2504
2505#define IsView(X) ((X)->eTabType==TABTYP_VIEW)
2506#define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
drh7d10d5a2008-08-20 16:35:102507
2508/*
drh4cbdda92006-06-14 19:00:202509** Test to see whether or not a table is a virtual table. This is
2510** done as a macro so that it will be optimized out when virtual
2511** table support is omitted from the build.
2512*/
2513#ifndef SQLITE_OMIT_VIRTUALTABLE
drhf38524d2021-08-02 16:41:572514# define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
drh78d1d222020-02-17 19:25:072515# define ExprIsVtab(X) \
drh63b3a642022-10-20 13:36:322516 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
drh4cbdda92006-06-14 19:00:202517#else
danielk1977034ca142007-06-26 10:38:542518# define IsVirtual(X) 0
drh78d1d222020-02-17 19:25:072519# define ExprIsVtab(X) 0
drh4cbdda92006-06-14 19:00:202520#endif
2521
drh03d69a62015-11-19 13:53:572522/*
2523** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
2524** only works for non-virtual tables (ordinary tables and views) and is
2525** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
2526** IsHiddenColumn() macro is general purpose.
2527*/
2528#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
2529# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
2530# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
drh18f8e732015-11-19 18:11:202531#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
drh03d69a62015-11-19 13:53:572532# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
2533# define IsOrdinaryHiddenColumn(X) 0
2534#else
2535# define IsHiddenColumn(X) 0
2536# define IsOrdinaryHiddenColumn(X) 0
2537#endif
2538
2539
drhec95c442013-10-23 01:57:322540/* Does the table have a rowid */
2541#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
drhfccda8a2015-05-27 13:06:552542#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
drhec95c442013-10-23 01:57:322543
drh4b42b522024-03-19 13:31:542544/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
2545** available. By default, this macro is false
2546*/
2547#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
2548# define ViewCanHaveRowid 0
2549#else
2550# define ViewCanHaveRowid (sqlite3Config.mNoVisibleRowid==0)
2551#endif
2552
drh4cbdda92006-06-14 19:00:202553/*
drhc2eef3b2002-08-31 18:53:062554** Each foreign key constraint is an instance of the following structure.
2555**
2556** A foreign key is associated with two tables. The "from" table is
2557** the table that contains the REFERENCES clause that creates the foreign
2558** key. The "to" table is the table that is named in the REFERENCES clause.
2559** Consider this example:
2560**
2561** CREATE TABLE ex1(
2562** a INTEGER PRIMARY KEY,
2563** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
2564** );
2565**
2566** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
drhbd50a922013-11-03 02:27:582567** Equivalent names:
2568**
2569** from-table == child-table
2570** to-table == parent-table
drhc2eef3b2002-08-31 18:53:062571**
2572** Each REFERENCES clause generates an instance of the following structure
2573** which is attached to the from-table. The to-table need not exist when
drhe61922a2009-05-02 13:29:372574** the from-table is created. The existence of the to-table is not checked.
drhbd50a922013-11-03 02:27:582575**
2576** The list of all parents for child Table X is held at X.pFKey.
2577**
2578** A list of all children for a table named Z (which might not even exist)
2579** is held in Schema.fkeyHash with a hash key of Z.
drhc2eef3b2002-08-31 18:53:062580*/
2581struct FKey {
drh1f638ce2009-09-24 13:48:102582 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
drhbd50a922013-11-03 02:27:582583 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
drh1f638ce2009-09-24 13:48:102584 char *zTo; /* Name of table that the key points to (aka: Parent) */
drhbd50a922013-11-03 02:27:582585 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
2586 FKey *pPrevTo; /* Previous with the same zTo */
drhc2eef3b2002-08-31 18:53:062587 int nCol; /* Number of columns in this key */
drh4c429832009-10-12 22:30:492588 /* EV: R-30323-21917 */
drhbd50a922013-11-03 02:27:582589 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
2590 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
2591 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
2592 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
2593 int iFrom; /* Index of column in pFrom */
2594 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
drhcebf06c2025-03-14 18:10:022595 } aCol[FLEXARRAY]; /* One entry for each of nCol columns */
drhc2eef3b2002-08-31 18:53:062596};
2597
drhcebf06c2025-03-14 18:10:022598/* The size (in bytes) of an FKey object holding N columns. The answer
2599** does NOT include space to hold the zTo name. */
2600#define SZ_FKEY(N) (offsetof(FKey,aCol)+(N)*sizeof(struct sColMap))
2601
drhc2eef3b2002-08-31 18:53:062602/*
danielk1977aaa40c12007-09-21 04:28:162603** SQLite supports many different ways to resolve a constraint
drh22f70c32002-02-18 01:17:002604** error. ROLLBACK processing means that a constraint violation
drh0bd1f4e2002-06-06 18:54:392605** causes the operation in process to fail and for the current transaction
drh1c928532002-01-31 15:54:212606** to be rolled back. ABORT processing means the operation in process
2607** fails and any prior changes from that one operation are backed out,
2608** but the transaction is not rolled back. FAIL processing means that
2609** the operation in progress stops and returns an error code. But prior
2610** changes due to the same operation are not backed out and no rollback
2611** occurs. IGNORE means that the particular row that caused the constraint
2612** error is not inserted or updated. Processing continues and no error
2613** is returned. REPLACE means that preexisting database rows that caused
2614** a UNIQUE constraint violation are removed so that the new insert or
2615** update can proceed. Processing continues and no error is reported.
drh56027772020-12-09 13:11:022616** UPDATE applies to insert operations only and means that the insert
2617** is omitted and the DO UPDATE clause of an upsert is run instead.
drhc2eef3b2002-08-31 18:53:062618**
drh56027772020-12-09 13:11:022619** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
drhc2eef3b2002-08-31 18:53:062620** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
2621** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
drh56027772020-12-09 13:11:022622** key is set to NULL. SETDFLT means that the foreign key is set
2623** to its default value. CASCADE means that a DELETE or UPDATE of the
drhc2eef3b2002-08-31 18:53:062624** referenced table row is propagated into the row that holds the
2625** foreign key.
mistachkinbfc9b3f2016-02-15 22:01:242626**
drh56027772020-12-09 13:11:022627** The OE_Default value is a place holder that means to use whatever
larrybrbc917382023-06-07 08:40:312628** conflict resolution algorithm is required from context.
drh56027772020-12-09 13:11:022629**
drh968af522003-02-11 14:55:402630** The following symbolic values are used to record which type
drh56027772020-12-09 13:11:022631** of conflict resolution action to take.
drh9cfcf5d2002-01-29 18:41:242632*/
drh1c928532002-01-31 15:54:212633#define OE_None 0 /* There is no constraint to check */
2634#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
2635#define OE_Abort 2 /* Back out changes but do no rollback transaction */
2636#define OE_Fail 3 /* Stop the operation but leave all prior changes */
2637#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
2638#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
drhc8a0c902018-04-13 15:14:332639#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
2640#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
2641#define OE_SetNull 8 /* Set the foreign key value to NULL */
2642#define OE_SetDflt 9 /* Set the foreign key value to its default */
2643#define OE_Cascade 10 /* Cascade the changes */
2644#define OE_Default 11 /* Do whatever the default action is */
drh9cfcf5d2002-01-29 18:41:242645
drhd3d39e92004-05-20 22:16:292646
2647/*
2648** An instance of the following structure is passed as the first
mistachkinbfc9b3f2016-02-15 22:01:242649** argument to sqlite3VdbeKeyCompare and is used to control the
drhd3d39e92004-05-20 22:16:292650** comparison of the two index keys.
drh323df792013-08-05 19:11:292651**
drh7590bfd2025-06-02 09:49:072652** The aSortOrder[] and aColl[] arrays have nAllField slots each. There
2653** are nKeyField slots for the columns of an index then extra slots
2654** for the rowid or key at the end. The aSortOrder array is located after
2655** the aColl[] array.
drh8658a8d2025-06-02 13:54:332656**
2657** If SQLITE_ENABLE_PREUPDATE_HOOK is defined, then aSortFlags might be NULL
2658** to indicate that this object is for use by a preupdate hook. When aSortFlags
2659** is NULL, then nAllField is uninitialized and no space is allocated for
2660** aColl[], so those fields may not be used.
drhd3d39e92004-05-20 22:16:292661*/
2662struct KeyInfo {
drh2ec2fb22013-11-06 19:59:232663 u32 nRef; /* Number of references to this KeyInfo object */
drh9b8d0272010-08-09 15:44:212664 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
drha485ad12017-08-02 22:43:142665 u16 nKeyField; /* Number of key columns in the index */
2666 u16 nAllField; /* Total columns, including key plus others */
drh2ec2fb22013-11-06 19:59:232667 sqlite3 *db; /* The database connection */
dan6e118922019-08-12 16:36:382668 u8 *aSortFlags; /* Sort order for each column. */
drhcebf06c2025-03-14 18:10:022669 CollSeq *aColl[FLEXARRAY]; /* Collating sequence for each term of the key */
drhd3d39e92004-05-20 22:16:292670};
2671
drh8658a8d2025-06-02 13:54:332672/* The size (in bytes) of a KeyInfo object with up to N fields. This includes
2673** the main body of the KeyInfo object and the aColl[] array of N elements,
2674** but does not count the memory used to hold aSortFlags[]. */
drhcebf06c2025-03-14 18:10:022675#define SZ_KEYINFO(N) (offsetof(KeyInfo,aColl) + (N)*sizeof(CollSeq*))
2676
drh8ae57fa2025-05-30 15:43:042677/* The size of a bare KeyInfo with no aColl[] entries */
2678#if FLEXARRAY+1 > 1
2679# define SZ_KEYINFO_0 offsetof(KeyInfo,aColl)
2680#else
2681# define SZ_KEYINFO_0 sizeof(KeyInfo)
2682#endif
2683
drh905d4722019-09-28 18:28:192684/*
2685** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
2686*/
2687#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */
2688#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */
dan6e118922019-08-12 16:36:382689
drh9cfcf5d2002-01-29 18:41:242690/*
drhb1d607d2015-11-05 22:30:542691** This object holds a record which has been parsed out into individual
2692** fields, for the purposes of doing a comparison.
drhe63d9992008-08-13 19:11:482693**
2694** A record is an object that contains one or more fields of data.
2695** Records are used to store the content of a table row and to store
2696** the key of an index. A blob encoding of a record is created by
shane467bcf32008-11-24 20:01:322697** the OP_MakeRecord opcode of the VDBE and is disassembled by the
drhe63d9992008-08-13 19:11:482698** OP_Column opcode.
2699**
drhb1d607d2015-11-05 22:30:542700** An instance of this object serves as a "key" for doing a search on
2701** an index b+tree. The goal of the search is to find the entry that
drh8658a8d2025-06-02 13:54:332702** is closest to the key described by this object. This object might hold
2703** just a prefix of the key. The number of fields is given by nField.
dan3833e932014-03-01 19:44:562704**
drhb1d607d2015-11-05 22:30:542705** The r1 and r2 fields are the values to return if this key is less than
2706** or greater than a key in the btree, respectively. These are normally
2707** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
2708** is in DESC order.
2709**
2710** The key comparison functions actually return default_rc when they find
2711** an equals comparison. default_rc can be -1, 0, or +1. If there are
2712** multiple entries in the b-tree with the same key (when only looking
drh8658a8d2025-06-02 13:54:332713** at the first nField elements) then default_rc can be set to -1 to
drhb1d607d2015-11-05 22:30:542714** cause the search to find the last match, or +1 to cause the search to
2715** find the first match.
2716**
2717** The key comparison functions will set eqSeen to true if they ever
2718** get and equal results when comparing this structure to a b-tree record.
2719** When default_rc!=0, the search might end up on the record immediately
2720** before the first match or immediately after the last match. The
2721** eqSeen field will indicate whether or not an exact match exists in the
2722** b-tree.
drhe63d9992008-08-13 19:11:482723*/
2724struct UnpackedRecord {
drh8658a8d2025-06-02 13:54:332725 KeyInfo *pKeyInfo; /* Comparison info for the index that is unpacked */
2726 Mem *aMem; /* Values for columns of the index */
drhf357caf2022-02-27 21:10:492727 union {
2728 char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */
2729 i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */
2730 } u;
2731 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
drhe63d9992008-08-13 19:11:482732 u16 nField; /* Number of entries in apMem[] */
drha9e0aeb2014-03-04 00:15:162733 i8 default_rc; /* Comparison result if keys are equal */
dan38fdead2014-04-01 10:19:022734 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
drh61ffb2c2017-07-26 10:04:512735 i8 r1; /* Value to return if (lhs < rhs) */
2736 i8 r2; /* Value to return if (lhs > rhs) */
drh70528d72015-11-05 20:25:092737 u8 eqSeen; /* True if an equality comparison has been seen */
drhe63d9992008-08-13 19:11:482738};
2739
drhe63d9992008-08-13 19:11:482740
2741/*
drh66b89c82000-11-28 20:47:172742** Each SQL index is represented in memory by an
drh75897232000-05-29 14:26:002743** instance of the following structure.
drh967e8b72000-06-21 13:59:102744**
2745** The columns of the table that are to be indexed are described
2746** by the aiColumn[] field of this structure. For example, suppose
2747** we have the following table and index:
2748**
2749** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
2750** CREATE INDEX Ex2 ON Ex1(c3,c1);
2751**
2752** In the Table structure describing Ex1, nCol==3 because there are
2753** three columns in the table. In the Index structure describing
2754** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
mistachkinbfc9b3f2016-02-15 22:01:242755** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
drh967e8b72000-06-21 13:59:102756** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
2757** The second column to be indexed (c1) has an index of 0 in
2758** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
drhea1ba172003-04-20 00:00:232759**
2760** The Index.onError field determines whether or not the indexed columns
2761** must be unique and what to do if they are not. When Index.onError=OE_None,
2762** it means this is not a unique index. Otherwise it is a unique index
drh5723c652022-10-22 13:49:352763** and the value of Index.onError indicates which conflict resolution
2764** algorithm to employ when an attempt is made to insert a non-unique
drhea1ba172003-04-20 00:00:232765** element.
danc5b73582015-05-26 11:53:142766**
drh5723c652022-10-22 13:49:352767** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
2768** for a fast test to see if an index can serve as a covering index.
2769** colNotIdxed has a 1 bit for every column of the original table that
2770** is *not* available in the index. Thus the expression
2771** "colUsed & colNotIdxed" will be non-zero if the index is not a
2772** covering index. The most significant bit of of colNotIdxed will always
2773** be true (note-20221022-a). If a column beyond the 63rd column of the
2774** table is used, the "colUsed & colNotIdxed" test will always be non-zero
2775** and we have to assume either that the index is not covering, or use
2776** an alternative (slower) algorithm to determine whether or not
2777** the index is covering.
2778**
danc5b73582015-05-26 11:53:142779** While parsing a CREATE TABLE or CREATE INDEX statement in order to
drhccb21132020-06-19 11:34:572780** generate VDBE code (as opposed to parsing one read from an sqlite_schema
danc5b73582015-05-26 11:53:142781** table as part of parsing an existing database schema), transient instances
2782** of this structure may be created. In this case the Index.tnum variable is
2783** used to store the address of a VDBE instruction, not a database page
2784** number (it cannot - the database page is not allocated until the VDBE
2785** program is executed). See convertToWithoutRowidTable() for details.
drh75897232000-05-29 14:26:002786*/
2787struct Index {
drhb376dae2013-01-01 14:01:282788 char *zName; /* Name of this index */
drhbbbdc832013-10-22 18:01:402789 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
dancfc9df72014-04-25 15:01:012790 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
drhb376dae2013-01-01 14:01:282791 Table *pTable; /* The SQL table being indexed */
2792 char *zColAff; /* String defining the affinity of each column */
2793 Index *pNext; /* The next index associated with the same table */
2794 Schema *pSchema; /* Schema containing this index */
2795 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
drhf19aa5f2015-12-30 16:51:202796 const char **azColl; /* Array of collation sequence names for index */
drh1fe05372013-07-31 18:12:262797 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
drh1f9ca2c2015-08-25 16:57:522798 ExprList *aColExpr; /* Column expressions */
drhabc38152020-07-22 13:38:042799 Pgno tnum; /* DB Page containing root of this index */
drhbf539c42013-10-05 18:16:022800 LogEst szIdxRow; /* Estimated average row size in bytes */
drhbbbdc832013-10-22 18:01:402801 u16 nKeyCol; /* Number of columns forming the key */
drhcc803b22025-02-21 20:35:372802 u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */
drhbf539c42013-10-05 18:16:022803 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh5f913ec2019-01-10 13:56:082804 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
drhb376dae2013-01-01 14:01:282805 unsigned bUnordered:1; /* Use this index for == or IN queries only */
drh7699d1c2013-06-04 12:42:292806 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
drh7f9c5db2013-10-23 00:32:582807 unsigned isResized:1; /* True if resizeIndexObject() has been called */
drhec95c442013-10-23 01:57:322808 unsigned isCovering:1; /* True if this is a covering index */
drhf9df2fb2014-11-15 19:08:132809 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
drha3928dd2017-02-17 15:26:362810 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
drh7e8515d2017-12-08 19:37:042811 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
drhbf9ff252019-05-14 00:43:132812 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
drhc7476732019-10-24 20:29:252813 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
drhe70d4582022-10-17 14:46:392814 unsigned bHasExpr:1; /* Index contains an expression, either a literal
2815 ** expression, or a reference to a VIRTUAL column */
drh175b8f02019-08-08 15:24:172816#ifdef SQLITE_ENABLE_STAT4
drh2b9cf662011-09-22 20:52:562817 int nSample; /* Number of elements in aSample[] */
drh790adfd2023-05-03 05:00:102818 int mxSample; /* Number of slots allocated to aSample[] */
dan8ad169a2013-08-12 20:14:042819 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
daneea568d2013-08-07 19:46:152820 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
drhfaacf172011-08-12 01:51:452821 IndexSample *aSample; /* Samples of the left-most key */
drh9f07cf72014-10-22 15:27:052822 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
2823 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
drhfaacf172011-08-12 01:51:452824#endif
drh5723c652022-10-22 13:49:352825 Bitmask colNotIdxed; /* Unindexed columns in pTab */
dan02fa4692009-08-17 17:06:582826};
2827
2828/*
drh48dd1d82014-05-27 18:18:582829** Allowed values for Index.idxType
2830*/
2831#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
2832#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
2833#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
drh5f913ec2019-01-10 13:56:082834#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */
drh48dd1d82014-05-27 18:18:582835
2836/* Return true if index X is a PRIMARY KEY index */
2837#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
2838
drh5f1d1d92014-07-31 22:59:042839/* Return true if index X is a UNIQUE index */
2840#define IsUniqueIndex(X) ((X)->onError!=OE_None)
2841
drh4b92f982015-09-29 17:20:142842/* The Index.aiColumn[] values are normally positive integer. But
2843** there are some negative values that have special meaning:
2844*/
2845#define XN_ROWID (-1) /* Indexed column is the rowid */
2846#define XN_EXPR (-2) /* Indexed column is an expression */
2847
drh48dd1d82014-05-27 18:18:582848/*
drh175b8f02019-08-08 15:24:172849** Each sample stored in the sqlite_stat4 table is represented in memory
drh74e7c8f2011-10-21 19:06:322850** using a structure of this type. See documentation at the top of the
2851** analyze.c source file for additional information.
dan02fa4692009-08-17 17:06:582852*/
2853struct IndexSample {
danf52bb8d2013-08-03 20:24:582854 void *p; /* Pointer to sampled record */
2855 int n; /* Size of record in bytes */
2856 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
2857 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
2858 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
drh75897232000-05-29 14:26:002859};
2860
2861/*
mistachkin8bee11a2018-10-29 17:53:232862** Possible values to use within the flags argument to sqlite3GetToken().
2863*/
2864#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */
2865#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */
2866
2867/*
drh75897232000-05-29 14:26:002868** Each token coming out of the lexer is an instance of
drh4b59ab52002-08-24 18:24:512869** this structure. Tokens are also used as part of an expression.
drh4efc4752004-01-16 15:55:372870**
drh4a2c7472018-08-13 15:09:482871** The memory that "z" points to is owned by other objects. Take care
2872** that the owner of the "z" string does not deallocate the string before
2873** the Token goes out of scope! Very often, the "z" points to some place
2874** in the middle of the Parse.zSql text. But it might also point to a
2875** static string.
drh75897232000-05-29 14:26:002876*/
2877struct Token {
drhb7916a72009-05-27 10:31:292878 const char *z; /* Text of the token. Not NULL-terminated! */
2879 unsigned int n; /* Number of characters in this token */
drh75897232000-05-29 14:26:002880};
2881
2882/*
drh13449892005-09-07 21:22:452883** An instance of this structure contains information needed to generate
2884** code for a SELECT that contains aggregate functions.
2885**
2886** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
drh16dc07f2020-05-24 00:30:382887** pointer to this structure. The Expr.iAgg field is the index in
drh13449892005-09-07 21:22:452888** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
2889** code for that node.
2890**
2891** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
2892** original Select structure that describes the SELECT statement. These
2893** fields do not need to be freed when deallocating the AggInfo structure.
2894*/
2895struct AggInfo {
2896 u8 directMode; /* Direct rendering mode means take data directly
2897 ** from source tables rather than from accumulators */
2898 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
2899 ** than the source table */
drhc52e9d92025-06-27 19:02:212900 u32 nSortingColumn; /* Number of columns in the sorting index */
drh13449892005-09-07 21:22:452901 int sortingIdx; /* Cursor number of the sorting index */
dan5134d132011-09-02 10:31:112902 int sortingIdxPTab; /* Cursor number of pseudo-table */
drh3c8e4382022-11-22 15:43:162903 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
drha4510172012-02-02 15:50:172904 ExprList *pGroupBy; /* The group by clause */
drh13449892005-09-07 21:22:452905 struct AggInfo_col { /* For each column used in source tables */
danielk19770817d0d2007-02-14 09:19:362906 Table *pTab; /* Source table */
drh81185a52020-06-09 13:38:122907 Expr *pCExpr; /* The original expression */
drh13449892005-09-07 21:22:452908 int iTable; /* Cursor number of the source table */
drhc52e9d92025-06-27 19:02:212909 int iColumn; /* Column number within the source table */
2910 int iSorterColumn; /* Column number in the sorting index */
drh13449892005-09-07 21:22:452911 } *aCol;
2912 int nColumn; /* Number of used entries in aCol[] */
drh13449892005-09-07 21:22:452913 int nAccumulator; /* Number of columns that show through to the output.
2914 ** Additional columns are used only as parameters to
2915 ** aggregate functions */
2916 struct AggInfo_func { /* For each aggregate function */
drh81185a52020-06-09 13:38:122917 Expr *pFExpr; /* Expression encoding the function */
drh13449892005-09-07 21:22:452918 FuncDef *pFunc; /* The aggregate function implementation */
shane467bcf32008-11-24 20:01:322919 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
dan9bfafa82021-03-13 17:21:242920 int iDistAddr; /* Address of OP_OpenEphemeral */
drh59a0d0b2023-10-18 18:11:112921 int iOBTab; /* Ephemeral table to implement ORDER BY */
2922 u8 bOBPayload; /* iOBTab has payload columns separate from key */
2923 u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */
drh07117f82023-12-14 13:58:502924 u8 bUseSubtype; /* Transfer subtype info through sorter */
drh13449892005-09-07 21:22:452925 } *aFunc;
2926 int nFunc; /* Number of entries in aFunc[] */
drhe26d4282020-06-09 11:59:152927 u32 selId; /* Select to which this AggInfo belongs */
drh98164c32022-12-20 01:48:432928#ifdef SQLITE_DEBUG
2929 Select *pSelect; /* SELECT statement that this AggInfo supports */
2930#endif
drh13449892005-09-07 21:22:452931};
2932
2933/*
drh7960da02022-11-25 13:08:202934** Macros to compute aCol[] and aFunc[] register numbers.
2935**
larrybrbc917382023-06-07 08:40:312936** These macros should not be used prior to the call to
drh7960da02022-11-25 13:08:202937** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
2938** The assert()s that are part of this macro verify that constraint.
drh3c8e4382022-11-22 15:43:162939*/
drh575a7b82024-08-06 10:29:412940#ifndef NDEBUG
drh7960da02022-11-25 13:08:202941#define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
2942#define AggInfoFuncReg(A,I) \
2943 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
drh575a7b82024-08-06 10:29:412944#else
2945#define AggInfoColumnReg(A,I) ((A)->iFirstReg+(I))
2946#define AggInfoFuncReg(A,I) \
2947 ((A)->iFirstReg+(A)->nColumn+(I))
2948#endif
drh3c8e4382022-11-22 15:43:162949
2950/*
drh8677d302009-11-04 13:17:142951** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
2952** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
2953** than 32767 we have to make it 32-bit. 16-bit is preferred because
2954** it uses less memory in the Expr object, which is a big memory user
2955** in systems with lots of prepared statements. And few applications
2956** need more than about 10 or 20 variables. But some extreme users want
drhefdba1a2020-02-12 20:50:202957** to have prepared statements with over 32766 variables, and for them
drh8677d302009-11-04 13:17:142958** the option is available (at compile-time).
2959*/
drhefdba1a2020-02-12 20:50:202960#if SQLITE_MAX_VARIABLE_NUMBER<32767
drh481aa742009-11-05 18:46:022961typedef i16 ynVar;
drh8677d302009-11-04 13:17:142962#else
2963typedef int ynVar;
2964#endif
2965
2966/*
drh75897232000-05-29 14:26:002967** Each node of an expression in the parse tree is an instance
drh22f70c32002-02-18 01:17:002968** of this structure.
2969**
danielk19776ab3a2e2009-02-19 14:39:252970** Expr.op is the opcode. The integer parser token codes are reused
2971** as opcodes here. For example, the parser defines TK_GE to be an integer
2972** code representing the ">=" operator. This same integer code is reused
drh22f70c32002-02-18 01:17:002973** to represent the greater-than-or-equal-to operator in the expression
2974** tree.
2975**
mistachkinbfc9b3f2016-02-15 22:01:242976** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
drh074a1312021-10-08 10:25:062977** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
2978** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
danielk19776ab3a2e2009-02-19 14:39:252979** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
drh074a1312021-10-08 10:25:062980** then Expr.u.zToken contains the name of the function.
drh22f70c32002-02-18 01:17:002981**
danielk19776ab3a2e2009-02-19 14:39:252982** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
2983** binary operator. Either or both may be NULL.
2984**
2985** Expr.x.pList is a list of arguments if the expression is an SQL function,
2986** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
2987** Expr.x.pSelect is used if the expression is a sub-select or an expression of
2988** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
mistachkinbfc9b3f2016-02-15 22:01:242989** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
danielk19776ab3a2e2009-02-19 14:39:252990** valid.
drh22f70c32002-02-18 01:17:002991**
2992** An expression of the form ID or ID.ID refers to a column in a table.
2993** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
2994** the integer cursor number of a VDBE cursor pointing to that table and
2995** Expr.iColumn is the column number for the specific column. If the
2996** expression is used as a result in an aggregate SELECT, then the
2997** value is also stored in the Expr.iAgg column in the aggregate so that
2998** it can be accessed after all aggregates are computed.
2999**
mistachkinbfc9b3f2016-02-15 22:01:243000** If the expression is an unbound variable marker (a question mark
3001** character '?' in the original SQL) then the Expr.iTable holds the index
danielk19776ab3a2e2009-02-19 14:39:253002** number for that variable.
drh22f70c32002-02-18 01:17:003003**
drh1398ad32005-01-19 23:24:503004** If the expression is a subquery then Expr.iColumn holds an integer
3005** register number containing the result of the subquery. If the
3006** subquery gives a constant result, then iTable is -1. If the subquery
3007** gives a different answer at different times during statement processing
3008** then iTable is the address of a subroutine that computes the subquery.
3009**
danielk1977aee18ef2005-03-09 12:26:503010** If the Expr is of type OP_Column, and the table it is selecting from
3011** is a disk table or the "old.*" pseudo-table, then pTab points to the
3012** corresponding table definition.
danielk19776ab3a2e2009-02-19 14:39:253013**
3014** ALLOCATION NOTES:
3015**
drh12ffee82009-04-08 13:51:513016** Expr objects can use a lot of memory space in database schema. To
3017** help reduce memory requirements, sometimes an Expr object will be
3018** truncated. And to reduce the number of memory allocations, sometimes
3019** two or more Expr objects will be stored in a single memory allocation,
drh074a1312021-10-08 10:25:063020** together with Expr.u.zToken strings.
danielk19776ab3a2e2009-02-19 14:39:253021**
drhb7916a72009-05-27 10:31:293022** If the EP_Reduced and EP_TokenOnly flags are set when
drh12ffee82009-04-08 13:51:513023** an Expr object is truncated. When EP_Reduced is set, then all
3024** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
3025** are contained within the same memory allocation. Note, however, that
3026** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
3027** allocated, regardless of whether or not EP_Reduced is set.
drh75897232000-05-29 14:26:003028*/
3029struct Expr {
drh1cc093c2002-06-24 22:01:573030 u8 op; /* Operation performed by this node */
drh11949042019-08-05 18:01:423031 char affExpr; /* affinity, or RAISE type */
drh20cee7d2019-10-30 18:50:083032 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
3033 ** TK_COLUMN: the value of p5 for OP_Column
3034 ** TK_AGG_FUNCTION: nesting depth
3035 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
drhe7375bf2020-03-10 19:24:383036#ifdef SQLITE_DEBUG
3037 u8 vvaFlags; /* Verification flags. */
3038#endif
drhc5cd1242013-09-12 16:50:493039 u32 flags; /* Various flags. EP_* See below */
drh33e619f2009-05-28 01:00:553040 union {
3041 char *zToken; /* Token value. Zero terminated and dequoted */
drhd50ffc42011-03-08 02:38:283042 int iValue; /* Non-negative integer value if EP_IntValue */
drh33e619f2009-05-28 01:00:553043 } u;
danielk19776ab3a2e2009-02-19 14:39:253044
3045 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
3046 ** space is allocated for the fields below this point. An attempt to
mistachkinbfc9b3f2016-02-15 22:01:243047 ** access them will result in a segfault or malfunction.
danielk19776ab3a2e2009-02-19 14:39:253048 *********************************************************************/
3049
danielk19776ab3a2e2009-02-19 14:39:253050 Expr *pLeft; /* Left subnode */
3051 Expr *pRight; /* Right subnode */
3052 union {
drhc5cd1242013-09-12 16:50:493053 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
3054 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
danielk19776ab3a2e2009-02-19 14:39:253055 } x;
danielk19776ab3a2e2009-02-19 14:39:253056
3057 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
3058 ** space is allocated for the fields below this point. An attempt to
3059 ** access them will result in a segfault or malfunction.
3060 *********************************************************************/
3061
drh6ec65492012-09-13 19:59:093062#if SQLITE_MAX_EXPR_DEPTH>0
3063 int nHeight; /* Height of the tree headed by this node */
3064#endif
drhb7916a72009-05-27 10:31:293065 int iTable; /* TK_COLUMN: cursor number of table holding column
dan2832ad42009-08-31 15:27:273066 ** TK_REGISTER: register number
drhcca9f3d2013-09-06 15:23:293067 ** TK_TRIGGER: 1 -> new, 0 -> old
drhfc7f27b2016-08-20 00:07:013068 ** EP_Unlikely: 134217728 times likelihood
larrybrbc917382023-06-07 08:40:313069 ** TK_IN: ephemeral table holding RHS
drh554a9dc2019-08-26 14:18:283070 ** TK_SELECT_COLUMN: Number of columns on the LHS
drhfc7f27b2016-08-20 00:07:013071 ** TK_SELECT: 1st register of result vector */
drh8677d302009-11-04 13:17:143072 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
drhfc7f27b2016-08-20 00:07:013073 ** TK_VARIABLE: variable number (always >= 1).
3074 ** TK_SELECT_COLUMN: column of the result vector */
drhb7916a72009-05-27 10:31:293075 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
drh796588a2022-02-05 21:49:473076 union {
drha6e8ee12022-05-13 16:38:403077 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
drh796588a2022-02-05 21:49:473078 int iOfst; /* else: start of token from start of statement */
3079 } w;
drh13449892005-09-07 21:22:453080 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
drheda079c2018-09-20 19:02:153081 union {
3082 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
3083 ** for a column of an index on an expression */
dan4f9adee2019-07-13 16:22:503084 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
drh74cc1092025-07-18 12:10:153085 int nReg; /* TK_NULLS: Number of registers to NULL out */
drh2c041312018-12-24 02:34:493086 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
3087 int iAddr; /* Subroutine entry address */
3088 int regReturn; /* Register used to hold return address */
3089 } sub;
drheda079c2018-09-20 19:02:153090 } y;
drh75897232000-05-29 14:26:003091};
3092
drh477572b2021-10-07 20:46:293093/* The following are the meanings of bits in the Expr.flags field.
drhd137f4e2019-03-29 01:15:113094** Value restrictions:
3095**
3096** EP_Agg == NC_HasAgg == SF_HasAgg
3097** EP_Win == NC_HasWin
drh1f162302002-10-27 19:35:333098*/
drh67a99db2022-05-13 14:52:043099#define EP_OuterON 0x000001 /* Originates in ON/USING clause of outer join */
3100#define EP_InnerON 0x000002 /* Originates in ON/USING of an inner join */
3101#define EP_Distinct 0x000004 /* Aggregate function with DISTINCT keyword */
3102#define EP_HasFunc 0x000008 /* Contains one or more functions of any kind */
drh42d2fce2019-08-15 20:04:093103#define EP_Agg 0x000010 /* Contains one or more aggregate functions */
drh67a99db2022-05-13 14:52:043104#define EP_FixedCol 0x000020 /* TK_Column with a known fixed value */
3105#define EP_VarSelect 0x000040 /* pSelect is correlated, not constant */
3106#define EP_DblQuoted 0x000080 /* token.z was originally in "..." */
3107#define EP_InfixFunc 0x000100 /* True for an infix function: LIKE, GLOB, etc */
3108#define EP_Collate 0x000200 /* Tree contains a TK_COLLATE operator */
3109#define EP_Commuted 0x000400 /* Comparison operator has been commuted */
3110#define EP_IntValue 0x000800 /* Integer value contained in u.iValue */
3111#define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
3112#define EP_Skip 0x002000 /* Operator does not contribute to affinity */
3113#define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
drh42d2fce2019-08-15 20:04:093114#define EP_Win 0x008000 /* Contains window functions */
drh67a99db2022-05-13 14:52:043115#define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
drh4e254642023-10-19 18:07:583116#define EP_FullSize 0x020000 /* Expr structure must remain full sized */
drh67a99db2022-05-13 14:52:043117#define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
3118#define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
3119#define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
3120#define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */
3121#define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */
drh42d2fce2019-08-15 20:04:093122#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
3123#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
3124#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
3125#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */
3126#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */
3127#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */
3128#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
drhccb21132020-06-19 11:34:573129#define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */
dand564bdb2024-10-05 18:10:023130#define EP_SubtArg 0x80000000 /* Is argument to SQLITE_SUBTYPE function */
drh885a5b02015-02-09 15:21:363131
drh477572b2021-10-07 20:46:293132/* The EP_Propagate mask is a set of properties that automatically propagate
drhfca23552017-10-28 20:51:543133** upwards into parent nodes.
drh885a5b02015-02-09 15:21:363134*/
drhfca23552017-10-28 20:51:543135#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
drh33e619f2009-05-28 01:00:553136
drh477572b2021-10-07 20:46:293137/* Macros can be used to test, set, or clear bits in the
drh1f162302002-10-27 19:35:333138** Expr.flags field.
3139*/
drhce250072025-02-21 17:03:223140#define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0)
3141#define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P))
3142#define ExprSetProperty(E,P) (E)->flags|=(u32)(P)
3143#define ExprClearProperty(E,P) (E)->flags&=~(u32)(P)
drh67a99db2022-05-13 14:52:043144#define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
3145#define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
drh4e254642023-10-19 18:07:583146#define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
drh1f162302002-10-27 19:35:333147
drh477572b2021-10-07 20:46:293148/* Macros used to ensure that the correct members of unions are accessed
3149** in Expr.
drha4eeccd2021-10-07 17:43:303150*/
drh477572b2021-10-07 20:46:293151#define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
3152#define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
drhe30ecbf2023-06-13 18:10:523153#define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
3154#define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
drh477572b2021-10-07 20:46:293155#define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
3156#define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
3157#define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
3158#define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
3159#define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
drhe7375bf2020-03-10 19:24:383160
3161/* Flags for use with Expr.vvaFlags
3162*/
3163#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
3164#define EP_Immutable 0x02 /* Do not change this Expr node */
3165
drhebb6a652013-09-12 23:42:223166/* The ExprSetVVAProperty() macro is used for Verification, Validation,
3167** and Accreditation only. It works like ExprSetProperty() during VVA
3168** processes but is a no-op for delivery.
3169*/
3170#ifdef SQLITE_DEBUG
drhe7375bf2020-03-10 19:24:383171# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
3172# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
3173# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
drhebb6a652013-09-12 23:42:223174#else
3175# define ExprSetVVAProperty(E,P)
drhe7375bf2020-03-10 19:24:383176# define ExprHasVVAProperty(E,P) 0
3177# define ExprClearVVAProperties(E)
drhebb6a652013-09-12 23:42:223178#endif
3179
drh1f162302002-10-27 19:35:333180/*
mistachkinbfc9b3f2016-02-15 22:01:243181** Macros to determine the number of bytes required by a normal Expr
3182** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
danielk19776ab3a2e2009-02-19 14:39:253183** and an Expr struct with the EP_TokenOnly flag set.
3184*/
drh12ffee82009-04-08 13:51:513185#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
3186#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
drhb7916a72009-05-27 10:31:293187#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
danielk19776ab3a2e2009-02-19 14:39:253188
3189/*
mistachkinbfc9b3f2016-02-15 22:01:243190** Flags passed to the sqlite3ExprDup() function. See the header comment
danielk19776ab3a2e2009-02-19 14:39:253191** above sqlite3ExprDup() for details.
3192*/
drh12ffee82009-04-08 13:51:513193#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
danielk19776ab3a2e2009-02-19 14:39:253194
3195/*
dan4f9adee2019-07-13 16:22:503196** True if the expression passed as an argument was a function with
3197** an OVER() clause (a window function).
3198*/
dan3703edf2019-10-10 15:17:093199#ifdef SQLITE_OMIT_WINDOWFUNC
3200# define IsWindowFunc(p) 0
3201#else
3202# define IsWindowFunc(p) ( \
dan4f9adee2019-07-13 16:22:503203 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
dan3703edf2019-10-10 15:17:093204 )
3205#endif
dan4f9adee2019-07-13 16:22:503206
3207/*
drh75897232000-05-29 14:26:003208** A list of expressions. Each expression may optionally have a
3209** name. An expr/name combination can be used in several ways, such
3210** as the list of "expr AS ID" fields following a "SELECT" or in the
3211** list of "ID = expr" items in an UPDATE. A list of expressions can
drhad3cab52002-05-24 02:04:323212** also be used as the argument to a function, in which case the a.zName
drh75897232000-05-29 14:26:003213** field is not used.
drh0dde4732012-12-19 13:41:033214**
drhcbb9da32019-12-12 22:11:333215** In order to try to keep memory usage down, the Expr.a.zEName field
3216** is used for multiple purposes:
3217**
drhc4938ea2019-12-13 00:49:423218** eEName Usage
3219** ---------- -------------------------
3220** ENAME_NAME (1) the AS of result set column
3221** (2) COLUMN= of an UPDATE
drhcbb9da32019-12-12 22:11:333222**
drhc4938ea2019-12-13 00:49:423223** ENAME_TAB DB.TABLE.NAME used to resolve names
3224** of subqueries
drhcbb9da32019-12-12 22:11:333225**
drhc4938ea2019-12-13 00:49:423226** ENAME_SPAN Text of the original result set
3227** expression.
drh75897232000-05-29 14:26:003228*/
3229struct ExprList {
3230 int nExpr; /* Number of expressions on the list */
drh50e43c52021-03-23 14:27:353231 int nAlloc; /* Number of a[] slots allocated */
drhd872bb12012-02-02 01:58:083232 struct ExprList_item { /* For each expression in the list */
drhc5f48162017-02-16 16:26:533233 Expr *pExpr; /* The parse tree for this expression */
drh41cee662019-12-12 20:22:343234 char *zEName; /* Token associated with this expression */
drhd88fd532022-05-02 20:49:303235 struct {
3236 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
3237 unsigned eEName :2; /* Meaning of zEName */
3238 unsigned done :1; /* Indicates when processing is finished */
3239 unsigned reusable :1; /* Constant expression is reusable */
3240 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
3241 unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */
3242 unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */
3243 unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */
3244 unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should
3245 ** not be expanded by "*" in parent queries */
3246 } fg;
drhc2acc4e2013-11-15 18:15:193247 union {
drhdbfbb5a2021-10-07 23:04:503248 struct { /* Used by any ExprList other than Parse.pConsExpr */
drhc2acc4e2013-11-15 18:15:193249 u16 iOrderByCol; /* For ORDER BY, column number in result set */
3250 u16 iAlias; /* Index into Parse.aAlias[] for zName */
3251 } x;
drhdbfbb5a2021-10-07 23:04:503252 int iConstExprReg; /* Register in which Expr value is cached. Used only
3253 ** by Parse.pConstExpr */
drhc2acc4e2013-11-15 18:15:193254 } u;
drhcebf06c2025-03-14 18:10:023255 } a[FLEXARRAY]; /* One slot for each expression in the list */
drh75897232000-05-29 14:26:003256};
3257
drhcebf06c2025-03-14 18:10:023258/* The size (in bytes) of an ExprList object that is big enough to hold
3259** as many as N expressions. */
3260#define SZ_EXPRLIST(N) \
3261 (offsetof(ExprList,a) + (N)*sizeof(struct ExprList_item))
3262
drh75897232000-05-29 14:26:003263/*
drhcbb9da32019-12-12 22:11:333264** Allowed values for Expr.a.eEName
3265*/
3266#define ENAME_NAME 0 /* The AS clause of a result set */
3267#define ENAME_SPAN 1 /* Complete text of the result set expression */
3268#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
dan81b70d92023-09-15 18:36:513269#define ENAME_ROWID 3 /* "DB.TABLE._rowid_" for * expansion of rowid */
drhcbb9da32019-12-12 22:11:333270
3271/*
drhad3cab52002-05-24 02:04:323272** An instance of this structure can hold a simple list of identifiers,
3273** such as the list "a,b,c" in the following statements:
3274**
3275** INSERT INTO t(a,b,c) VALUES ...;
3276** CREATE INDEX idx ON t(a,b,c);
3277** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
3278**
3279** The IdList.a.idx field is used when the IdList represents the list of
3280** column names after a table name in an INSERT statement. In the statement
3281**
3282** INSERT INTO t(a,b,c) ...
3283**
3284** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
drh75897232000-05-29 14:26:003285*/
3286struct IdList {
drha99e3252022-04-15 15:47:143287 int nId; /* Number of identifiers on the list */
drh6d4abfb2001-10-22 02:58:083288 struct IdList_item {
drhad3cab52002-05-24 02:04:323289 char *zName; /* Name of the identifier */
drhcebf06c2025-03-14 18:10:023290 } a[FLEXARRAY];
drhad3cab52002-05-24 02:04:323291};
3292
drhcebf06c2025-03-14 18:10:023293/* The size (in bytes) of an IdList object that can hold up to N IDs. */
3294#define SZ_IDLIST(N) (offsetof(IdList,a)+(N)*sizeof(struct IdList_item))
3295
drhad3cab52002-05-24 02:04:323296/*
drha99e3252022-04-15 15:47:143297** Allowed values for IdList.eType, which determines which value of the a.u4
3298** is valid.
3299*/
3300#define EU4_NONE 0 /* Does not use IdList.a.u4 */
3301#define EU4_IDX 1 /* Uses IdList.a.u4.idx */
drhf80bb192022-04-18 19:48:313302#define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
drha99e3252022-04-15 15:47:143303
3304/*
drh1521ca42024-08-19 22:48:303305** Details of the implementation of a subquery.
3306*/
3307struct Subquery {
3308 Select *pSelect; /* A SELECT statement used in place of a table name */
3309 int addrFillSub; /* Address of subroutine to initialize a subquery */
3310 int regReturn; /* Register holding return address of addrFillSub */
3311 int regResult; /* Registers holding results of a co-routine */
3312};
3313
3314/*
drh76012942021-02-21 21:04:543315** The SrcItem object represents a single term in the FROM clause of a query.
3316** The SrcList object is mostly an array of SrcItems.
drh074a1312021-10-08 10:25:063317**
drh5723c652022-10-22 13:49:353318** The jointype starts out showing the join type between the current table
3319** and the next table on the list. The parser builds the list this way.
3320** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
3321** jointype expresses the join between the table and the previous table.
3322**
3323** In the colUsed field, the high-order bit (bit 63) is set if the table
3324** contains more than 63 columns and the 64-th or later column is used.
3325**
drh692c1602024-08-20 19:09:593326** Aggressive use of "union" helps keep the size of the object small. This
3327** has been shown to boost performance, in addition to saving memory.
3328** Access to union elements is gated by the following rules which should
3329** always be checked, either by an if-statement or by an assert().
drh1521ca42024-08-19 22:48:303330**
drh692c1602024-08-20 19:09:593331** Field Only access if this is true
3332** --------------- -----------------------------------
drh1521ca42024-08-19 22:48:303333** u1.zIndexedBy fg.isIndexedBy
3334** u1.pFuncArg fg.isTabFunc
drhac7c6f52024-03-18 13:31:243335** u1.nRow !fg.isTabFunc && !fg.isIndexedBy
3336**
drh692c1602024-08-20 19:09:593337** u2.pIBIndex fg.isIndexedBy
3338** u2.pCteUse fg.isCte
drhb204b6a2024-08-17 23:23:233339**
drh692c1602024-08-20 19:09:593340** u3.pOn !fg.isUsing
3341** u3.pUsing fg.isUsing
drhb204b6a2024-08-17 23:23:233342**
drh692c1602024-08-20 19:09:593343** u4.zDatabase !fg.fixedSchema && !fg.isSubquery
3344** u4.pSchema fg.fixedSchema
drh1521ca42024-08-19 22:48:303345** u4.pSubq fg.isSubquery
drh692c1602024-08-20 19:09:593346**
3347** See also the sqlite3SrcListDelete() routine for assert() statements that
3348** check invariants on the fields of this object, especially the flags
3349** inside the fg struct.
drh76012942021-02-21 21:04:543350*/
3351struct SrcItem {
drh76012942021-02-21 21:04:543352 char *zName; /* Name of the table */
3353 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
drhb204b6a2024-08-17 23:23:233354 Table *pSTab; /* Table object for zName. Mnemonic: Srcitem-TABle */
drh76012942021-02-21 21:04:543355 struct {
3356 u8 jointype; /* Type of join between this table and the previous */
3357 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
3358 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
drh1521ca42024-08-19 22:48:303359 unsigned isSubquery :1; /* True if this term is a subquery */
drh76012942021-02-21 21:04:543360 unsigned isTabFunc :1; /* True if table-valued-function syntax */
3361 unsigned isCorrelated :1; /* True if sub-query is correlated */
drh40822eb2022-05-21 18:03:333362 unsigned isMaterialized:1; /* This is a materialized view */
drh76012942021-02-21 21:04:543363 unsigned viaCoroutine :1; /* Implemented as a co-routine */
3364 unsigned isRecursive :1; /* True for recursive reference in WITH */
3365 unsigned fromDDL :1; /* Comes from sqlite_schema */
drha79e2a22021-02-21 23:44:143366 unsigned isCte :1; /* This is a CTE */
drhcd1499f2021-05-20 00:44:043367 unsigned notCte :1; /* This item may not match a CTE */
drhd44f8b22022-04-07 01:11:133368 unsigned isUsing :1; /* u3.pUsing is valid */
drh5c118e32022-06-08 15:30:393369 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
larrybrbc917382023-06-07 08:40:313370 unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
drh815b7822022-04-20 15:07:393371 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
drh04624992024-05-18 20:00:083372 unsigned rowidUsed :1; /* The ROWID of this table is referenced */
drh8797bd62024-08-17 19:46:493373 unsigned fixedSchema :1; /* Uses u4.pSchema, not u4.zDatabase */
drh692c1602024-08-20 19:09:593374 unsigned hadSchema :1; /* Had u4.zDatabase before u4.pSchema */
drhaa54d7a2025-07-02 20:46:023375 unsigned fromExists :1; /* Comes from WHERE EXISTS(...) */
drh76012942021-02-21 21:04:543376 } fg;
3377 int iCursor; /* The VDBE cursor number used to access this table */
drh5723c652022-10-22 13:49:353378 Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
drh76012942021-02-21 21:04:543379 union {
3380 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
3381 ExprList *pFuncArg; /* Arguments to table-valued-function */
drh27a5ee82024-03-18 12:49:303382 u32 nRow; /* Number of rows in a VALUES clause */
drh76012942021-02-21 21:04:543383 } u1;
drha79e2a22021-02-21 23:44:143384 union {
3385 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
drh7704a532022-10-24 18:42:453386 CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
drha79e2a22021-02-21 23:44:143387 } u2;
drh8797bd62024-08-17 19:46:493388 union {
3389 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
3390 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
3391 } u3;
3392 union {
3393 Schema *pSchema; /* Schema to which this item is fixed */
3394 char *zDatabase; /* Name of database holding this table */
drh1521ca42024-08-19 22:48:303395 Subquery *pSubq; /* Description of a subquery */
drh8797bd62024-08-17 19:46:493396 } u4;
drh76012942021-02-21 21:04:543397};
3398
3399/*
drhd44f8b22022-04-07 01:11:133400** The OnOrUsing object represents either an ON clause or a USING clause.
3401** It can never be both at the same time, but it can be neither.
3402*/
3403struct OnOrUsing {
3404 Expr *pOn; /* The ON clause of a join */
3405 IdList *pUsing; /* The USING clause of a join */
3406};
3407
3408/*
drh5723c652022-10-22 13:49:353409** This object represents one or more tables that are the source of
3410** content for an SQL statement. For example, a single SrcList object
3411** is used to hold the FROM clause of a SELECT statement. SrcList also
3412** represents the target tables for DELETE, INSERT, and UPDATE statements.
drhd24cc422003-03-27 12:51:243413**
drhad3cab52002-05-24 02:04:323414*/
3415struct SrcList {
drhcebf06c2025-03-14 18:10:023416 int nSrc; /* Number of tables or subqueries in the FROM clause */
3417 u32 nAlloc; /* Number of entries allocated in a[] below */
3418 SrcItem a[FLEXARRAY]; /* One entry for each identifier on the list */
drh75897232000-05-29 14:26:003419};
3420
drhcebf06c2025-03-14 18:10:023421/* Size (in bytes) of a SrcList object that can hold as many as N
3422** SrcItem objects. */
3423#define SZ_SRCLIST(N) (offsetof(SrcList,a)+(N)*sizeof(SrcItem))
3424
3425/* Size (in bytes( of a SrcList object that holds 1 SrcItem. This is a
3426** special case of SZ_SRCITEM(1) that comes up often. */
3427#define SZ_SRCLIST_1 (offsetof(SrcList,a)+sizeof(SrcItem))
3428
drh75897232000-05-29 14:26:003429/*
drh01f3f252002-05-24 16:14:153430** Permitted values of the SrcList.a.jointype field
3431*/
drha76ac882022-04-08 19:20:123432#define JT_INNER 0x01 /* Any kind of inner or cross join */
3433#define JT_CROSS 0x02 /* Explicit use of the CROSS keyword */
3434#define JT_NATURAL 0x04 /* True for a "natural" join */
3435#define JT_LEFT 0x08 /* Left outer join */
3436#define JT_RIGHT 0x10 /* Right outer join */
3437#define JT_OUTER 0x20 /* The "OUTER" keyword is present */
drh4d0d0712022-04-19 19:51:513438#define JT_LTORJ 0x40 /* One of the LEFT operands of a RIGHT JOIN
3439 ** Mnemonic: Left Table Of Right Join */
drha76ac882022-04-08 19:20:123440#define JT_ERROR 0x80 /* unknown or unsupported join type */
drh01f3f252002-05-24 16:14:153441
drh111a6a72008-12-21 03:51:163442/*
drh336a5302009-04-24 15:46:213443** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
3444** and the WhereInfo.wctrlFlags member.
drh49711602016-04-14 16:40:133445**
3446** Value constraints (enforced via assert()):
3447** WHERE_USE_LIMIT == SF_FixedLimit
drh08c88eb2008-04-10 13:33:183448*/
drh6df2acd2008-12-28 16:55:253449#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
3450#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
3451#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
3452#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
drhce943bc2016-05-19 18:56:333453#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
3454#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
drhbc5eac02016-05-19 19:31:303455#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
3456 ** the OR optimization */
drhce943bc2016-05-19 18:56:333457#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
3458#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
3459#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
3460#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
danf330d532021-04-03 19:23:593461#define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
drhd711e522016-05-19 22:40:043462#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
drhc5837192022-04-11 14:26:373463#define WHERE_RIGHT_JOIN 0x1000 /* Processing a RIGHT JOIN */
drh2fbb3fb2024-06-09 17:34:033464#define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */
drhbc5eac02016-05-19 19:31:303465#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
drhce943bc2016-05-19 18:56:333466 /* 0x8000 not currently used */
danielk1977a9d1ccb2008-01-05 17:39:293467
drh4f402f22013-06-11 18:59:383468/* Allowed return values from sqlite3WhereIsDistinct()
3469*/
drhe8e4af72012-09-21 00:04:283470#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
3471#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
3472#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
3473#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
dan38cc40c2011-06-30 20:17:153474
drh75897232000-05-29 14:26:003475/*
danielk1977b3bce662005-01-29 08:32:433476** A NameContext defines a context in which to resolve table and column
3477** names. The context consists of a list of tables (the pSrcList) field and
3478** a list of named expression (pEList). The named expression list may
3479** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
3480** to the table being operated on by INSERT, UPDATE, or DELETE. The
3481** pEList corresponds to the result set of a SELECT and is NULL for
3482** other statements.
3483**
mistachkinbfc9b3f2016-02-15 22:01:243484** NameContexts can be nested. When resolving names, the inner-most
danielk1977b3bce662005-01-29 08:32:433485** context is searched first. If no match is found, the next outer
3486** context is checked. If there is still no match, the next context
3487** is checked. This process continues until either a match is found
3488** or all contexts are check. When a match is found, the nRef member of
mistachkinbfc9b3f2016-02-15 22:01:243489** the context containing the match is incremented.
danielk1977b3bce662005-01-29 08:32:433490**
3491** Each subquery gets a new NameContext. The pNext field points to the
3492** NameContext in the parent query. Thus the process of scanning the
3493** NameContext list corresponds to searching through successively outer
3494** subqueries looking for a match.
3495*/
3496struct NameContext {
3497 Parse *pParse; /* The parser */
3498 SrcList *pSrcList; /* One or more tables used to resolve names */
drh25c3b8c2018-04-16 10:34:133499 union {
3500 ExprList *pEList; /* Optional list of result-set columns */
3501 AggInfo *pAggInfo; /* Information about aggregates at this level */
drheac9fab2018-04-16 13:00:503502 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
drh552562c2021-02-04 20:52:203503 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
drh25c3b8c2018-04-16 10:34:133504 } uNC;
danielk1977b3bce662005-01-29 08:32:433505 NameContext *pNext; /* Next outer name context. NULL for outermost */
drha51009b2012-05-21 19:11:253506 int nRef; /* Number of names resolved by this context */
drh050611a2021-04-10 13:37:043507 int nNcErr; /* Number of errors encountered while resolving names */
dan0d925712019-05-20 17:14:253508 int ncFlags; /* Zero or more NC_* flags defined below */
drh792103a2023-11-02 22:11:353509 u32 nNestedSelect; /* Number of nested selects using this NC */
dane3bf6322018-06-08 20:58:273510 Select *pWinSelect; /* SELECT statement for any window functions */
danielk1977b3bce662005-01-29 08:32:433511};
3512
3513/*
drha51009b2012-05-21 19:11:253514** Allowed values for the NameContext, ncFlags field.
drh9588ad92014-09-15 14:46:023515**
drh49711602016-04-14 16:40:133516** Value constraints (all checked via assert()):
drhbb301232021-07-15 19:29:433517** NC_HasAgg == SF_HasAgg == EP_Agg
3518** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
3519** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
drhd137f4e2019-03-29 01:15:113520** NC_HasWin == EP_Win
mistachkinbfc9b3f2016-02-15 22:01:243521**
drha51009b2012-05-21 19:11:253522*/
drhbb301232021-07-15 19:29:433523#define NC_AllowAgg 0x000001 /* Aggregate functions are allowed here */
3524#define NC_PartIdx 0x000002 /* True if resolving a partial index WHERE */
3525#define NC_IsCheck 0x000004 /* True if resolving a CHECK constraint */
3526#define NC_GenCol 0x000008 /* True for a GENERATED ALWAYS AS clause */
3527#define NC_HasAgg 0x000010 /* One or more aggregate functions seen */
3528#define NC_IdxExpr 0x000020 /* True if resolving columns of CREATE INDEX */
3529#define NC_SelfRef 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
drhffcad582023-03-15 17:58:513530#define NC_Subquery 0x000040 /* A subquery has been seen */
drhbb301232021-07-15 19:29:433531#define NC_UEList 0x000080 /* True if uNC.pEList is used */
3532#define NC_UAggInfo 0x000100 /* True if uNC.pAggInfo is used */
3533#define NC_UUpsert 0x000200 /* True if uNC.pUpsert is used */
3534#define NC_UBaseReg 0x000400 /* True if uNC.iBaseReg is used */
3535#define NC_MinMaxAgg 0x001000 /* min/max aggregates seen. See note above */
drh586b2b22024-06-03 12:36:433536/* 0x002000 // available for reuse */
drhbb301232021-07-15 19:29:433537#define NC_AllowWin 0x004000 /* Window functions are allowed here */
3538#define NC_HasWin 0x008000 /* One or more window functions seen */
3539#define NC_IsDDL 0x010000 /* Resolving names in a CREATE statement */
3540#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */
3541#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */
3542#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
drh61b77a62024-03-08 21:37:183543#define NC_Where 0x100000 /* Processing WHERE clause of a SELECT */
drhbb301232021-07-15 19:29:433544#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */
drha51009b2012-05-21 19:11:253545
3546/*
drh46d2e5c2018-04-12 13:15:433547** An instance of the following object describes a single ON CONFLICT
drhe9c2e772018-04-13 13:06:453548** clause in an upsert.
drh788d55a2018-04-13 01:15:093549**
3550** The pUpsertTarget field is only set if the ON CONFLICT clause includes
3551** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
drhe9c2e772018-04-13 13:06:453552** conflict-target clause.) The pUpsertTargetWhere is the optional
3553** WHERE clause used to identify partial unique indexes.
drh788d55a2018-04-13 01:15:093554**
larrybrbc917382023-06-07 08:40:313555** pUpsertSet is the list of column=expr terms of the UPDATE statement.
drh788d55a2018-04-13 01:15:093556** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
3557** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
3558** WHERE clause is omitted.
drh46d2e5c2018-04-12 13:15:433559*/
3560struct Upsert {
drh56027772020-12-09 13:11:023561 ExprList *pUpsertTarget; /* Optional description of conflict target */
drhe9c2e772018-04-13 13:06:453562 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
drh46d2e5c2018-04-12 13:15:433563 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
drhdab0eb52018-04-12 17:28:063564 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
drh2549e4c2020-12-08 14:29:033565 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
drh255c1c12020-12-12 00:28:153566 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
drh926fb602024-03-08 14:01:483567 u8 isDup; /* True if 2nd or later with same pUpsertIdx */
drhe84ad922020-12-09 20:30:473568 /* Above this point is the parse tree for the ON CONFLICT clauses.
3569 ** The next group of fields stores intermediate data. */
drhdaf27612020-12-10 20:31:253570 void *pToFree; /* Free memory when deleting the Upsert object */
drhe84ad922020-12-09 20:30:473571 /* All fields above are owned by the Upsert object and must be freed
3572 ** when the Upsert is destroyed. The fields below are used to transfer
3573 ** information from the INSERT processing down into the UPDATE processing
3574 ** while generating code. The fields below are owned by the INSERT
3575 ** statement and will be freed by INSERT processing. */
drh91f27172020-12-10 12:49:263576 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
drh0b30a112018-04-13 21:55:223577 SrcList *pUpsertSrc; /* Table to be updated */
drheac9fab2018-04-16 13:00:503578 int regData; /* First register holding array of VALUES */
drh7fc3aba2018-04-20 13:18:513579 int iDataCur; /* Index of the data cursor */
3580 int iIdxCur; /* Index of the first index cursor */
drh46d2e5c2018-04-12 13:15:433581};
3582
3583/*
drh9bb61fe2000-06-05 16:01:393584** An instance of the following structure contains all information
3585** needed to generate code for a single SELECT statement.
drha76b5df2002-02-23 02:32:103586**
drhbbd4ae52018-04-30 19:32:493587** See the header comment on the computeLimitRegisters() routine for a
3588** detailed description of the meaning of the iLimit and iOffset fields.
drh0342b1f2005-09-01 03:07:443589**
drhb9bb7c12006-06-11 23:41:553590** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
drh0342b1f2005-09-01 03:07:443591** These addresses must be stored so that we can go back and fill in
drh66a51672008-01-03 00:01:233592** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
drh0342b1f2005-09-01 03:07:443593** the number of columns in P2 can be computed at the same time
drhb9bb7c12006-06-11 23:41:553594** as the OP_OpenEphm instruction is coded because not
drh0342b1f2005-09-01 03:07:443595** enough information about the compound query is known at that point.
drhb9bb7c12006-06-11 23:41:553596** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
drh2c797332012-09-20 14:26:223597** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
drh0342b1f2005-09-01 03:07:443598** sequences for the ORDER BY clause.
drh9bb61fe2000-06-05 16:01:393599*/
3600struct Select {
drh7b58dae2003-07-20 01:16:463601 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
drhc3489bb2016-02-25 16:04:593602 LogEst nSelectRow; /* Estimated number of result rows */
3603 u32 selFlags; /* Various SF_* values */
drha4510172012-02-02 15:50:173604 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
drhfef37762018-07-10 19:48:353605 u32 selId; /* Unique identifier number for this SELECT */
drh079a3072014-03-19 14:10:553606 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
drha9ebfe22019-12-25 23:54:213607 ExprList *pEList; /* The fields of the result */
drhad3cab52002-05-24 02:04:323608 SrcList *pSrc; /* The FROM clause */
drh9bb61fe2000-06-05 16:01:393609 Expr *pWhere; /* The WHERE clause */
3610 ExprList *pGroupBy; /* The GROUP BY clause */
3611 Expr *pHaving; /* The HAVING clause */
3612 ExprList *pOrderBy; /* The ORDER BY clause */
drh967e8b72000-06-21 13:59:103613 Select *pPrior; /* Prior select in a compound select statement */
drh1e281292007-12-13 03:45:073614 Select *pNext; /* Next select to the left in a compound */
danielk1977a2dc3b12005-02-05 12:48:483615 Expr *pLimit; /* LIMIT expression. NULL means not used. */
dan4e9119d2014-01-13 15:12:233616 With *pWith; /* WITH clause attached to this select. Or NULL. */
dan67a9b8e2018-06-22 20:51:353617#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:073618 Window *pWin; /* List of window functions */
dane3bf6322018-06-08 20:58:273619 Window *pWinDefn; /* List of named window definitions */
dan67a9b8e2018-06-22 20:51:353620#endif
drh9bb61fe2000-06-05 16:01:393621};
3622
3623/*
drh7d10d5a2008-08-20 16:35:103624** Allowed values for Select.selFlags. The "SF" prefix stands for
3625** "Select Flag".
drh49711602016-04-14 16:40:133626**
3627** Value constraints (all checked via assert())
drhbb301232021-07-15 19:29:433628** SF_HasAgg == NC_HasAgg
3629** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
3630** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
3631** SF_FixedLimit == WHERE_USE_LIMIT
drh7d10d5a2008-08-20 16:35:103632*/
drhba016342019-11-14 13:24:043633#define SF_Distinct 0x0000001 /* Output should be DISTINCT */
3634#define SF_All 0x0000002 /* Includes the ALL keyword */
3635#define SF_Resolved 0x0000004 /* Identifiers have been resolved */
3636#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */
3637#define SF_HasAgg 0x0000010 /* Contains aggregate functions */
3638#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */
3639#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */
3640#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */
3641#define SF_Compound 0x0000100 /* Part of a compound query */
3642#define SF_Values 0x0000200 /* Synthesized from VALUES clause */
3643#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */
3644#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */
3645#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */
3646#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */
3647#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */
3648#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */
3649#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */
3650#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */
3651#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */
3652#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */
3653#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */
dan38096962019-12-09 08:13:433654#define SF_View 0x0200000 /* SELECT statement is a view */
drhb7cbf5c2020-06-15 13:51:343655#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */
dan5daf69e2021-07-05 11:27:133656#define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */
drh05c6d132024-04-07 10:27:183657#define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
dan903fdd42021-02-22 20:56:133658#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
danac67f562021-06-14 20:08:483659#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
drhee612e22021-07-16 20:16:193660#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
drh93f41e22022-12-09 18:26:153661#define SF_UpdateFrom 0x10000000 /* Query originates with UPDATE FROM */
drhde6a4be2024-04-06 12:19:503662#define SF_Correlated 0x20000000 /* True if references the outer context */
drh7d10d5a2008-08-20 16:35:103663
drhbb36d552024-08-20 22:05:013664/* True if SrcItem X is a subquery that has SF_NestedFrom */
drh1521ca42024-08-19 22:48:303665#define IsNestedFrom(X) \
3666 ((X)->fg.isSubquery && \
3667 ((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom)!=0)
drh815b7822022-04-20 15:07:393668
drh7d10d5a2008-08-20 16:35:103669/*
drh340309f2014-01-22 00:23:493670** The results of a SELECT can be distributed in several ways, as defined
3671** by one of the following macros. The "SRT" prefix means "SELECT Result
3672** Type".
3673**
mistachkinbfc9b3f2016-02-15 22:01:243674** SRT_Union Store results as a key in a temporary index
drh340309f2014-01-22 00:23:493675** identified by pDest->iSDParm.
3676**
3677** SRT_Except Remove results from the temporary index pDest->iSDParm.
3678**
3679** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
3680** set is not empty.
3681**
3682** SRT_Discard Throw the results away. This is used by SELECT
3683** statements within triggers whose only purpose is
3684** the side-effects of functions.
3685**
drh340309f2014-01-22 00:23:493686** SRT_Output Generate a row of output (using the OP_ResultRow
3687** opcode) for each row in the result set.
3688**
3689** SRT_Mem Only valid if the result is a single column.
3690** Store the first column of the first result row
3691** in register pDest->iSDParm then abandon the rest
3692** of the query. This destination implies "LIMIT 1".
3693**
3694** SRT_Set The result must be a single column. Store each
mistachkinbfc9b3f2016-02-15 22:01:243695** row of result as the key in table pDest->iSDParm.
drh340309f2014-01-22 00:23:493696** Apply the affinity pDest->affSdst before storing
drh6172e432024-07-03 17:51:483697** results. if pDest->iSDParm2 is positive, then it is
stephan624cb962024-10-19 12:39:063698** a register holding a Bloom filter for the IN operator
3699** that should be populated in addition to the
drh6172e432024-07-03 17:51:483700** pDest->iSDParm table. This SRT is used to
3701** implement "IN (SELECT ...)".
drh340309f2014-01-22 00:23:493702**
drh340309f2014-01-22 00:23:493703** SRT_EphemTab Create an temporary table pDest->iSDParm and store
3704** the result there. The cursor is left open after
3705** returning. This is like SRT_Table except that
3706** this destination uses OP_OpenEphemeral to create
3707** the table first.
3708**
3709** SRT_Coroutine Generate a co-routine that returns a new row of
3710** results each time it is invoked. The entry point
3711** of the co-routine is stored in register pDest->iSDParm
3712** and the result row is stored in pDest->nDest registers
3713** starting with pDest->iSdst.
3714**
drh781def22014-01-22 13:35:533715** SRT_Table Store results in temporary table pDest->iSDParm.
drh8e1ee882014-03-21 19:56:093716** SRT_Fifo This is like SRT_EphemTab except that the table
3717** is assumed to already be open. SRT_Fifo has
3718** the additional property of being able to ignore
3719** the ORDER BY clause.
drh781def22014-01-22 13:35:533720**
drh8e1ee882014-03-21 19:56:093721** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
drh340309f2014-01-22 00:23:493722** But also use temporary table pDest->iSDParm+1 as
3723** a record of all prior results and ignore any duplicate
drh8e1ee882014-03-21 19:56:093724** rows. Name means: "Distinct Fifo".
drh781def22014-01-22 13:35:533725**
3726** SRT_Queue Store results in priority queue pDest->iSDParm (really
3727** an index). Append a sequence number so that all entries
3728** are distinct.
3729**
3730** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
3731** the same record has never been stored before. The
3732** index at pDest->iSDParm+1 hold all prior stores.
dan243210b2020-07-15 15:32:593733**
3734** SRT_Upfrom Store results in the temporary table already opened by
3735** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
3736** table is an intkey table - in this case the first
3737** column returned by the SELECT is used as the integer
3738** key. If (pDest->iSDParm>0), then the table is an index
3739** table. (pDest->iSDParm) is the number of key columns in
3740** each index record in this case.
drhfef52082000-06-06 01:50:433741*/
drh13449892005-09-07 21:22:453742#define SRT_Union 1 /* Store result as keys in an index */
3743#define SRT_Except 2 /* Remove result from a UNION index */
danielk19779ed1dfa2008-01-02 17:11:143744#define SRT_Exists 3 /* Store 1 if the result is not empty */
3745#define SRT_Discard 4 /* Do not save the results anywhere */
drhf1ea4252020-09-17 00:46:093746#define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */
3747#define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */
3748
3749/* The DISTINCT clause is ignored for all of the above. Not that
3750** IgnorableDistinct() implies IgnorableOrderby() */
3751#define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
3752
drh8e1ee882014-03-21 19:56:093753#define SRT_Queue 7 /* Store result in an queue */
drhf1ea4252020-09-17 00:46:093754#define SRT_Fifo 8 /* Store result as data with an automatic rowid */
drhfef52082000-06-06 01:50:433755
drh13449892005-09-07 21:22:453756/* The ORDER BY clause is ignored for all of the above */
drhf1ea4252020-09-17 00:46:093757#define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
drh13449892005-09-07 21:22:453758
drh8e1ee882014-03-21 19:56:093759#define SRT_Output 9 /* Output each row of result */
3760#define SRT_Mem 10 /* Store result in a memory cell */
3761#define SRT_Set 11 /* Store results as keys in an index */
3762#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
3763#define SRT_Coroutine 13 /* Generate a single row of result */
3764#define SRT_Table 14 /* Store result as data with an automatic rowid */
danf2972b62020-04-29 20:11:013765#define SRT_Upfrom 15 /* Store result as data with rowid */
drh22827922000-06-06 17:27:053766
3767/*
drh634d81d2012-09-20 15:41:313768** An instance of this object describes where to put of the results of
3769** a SELECT statement.
danielk19776c8c8ce2008-01-02 16:27:093770*/
danielk19776c8c8ce2008-01-02 16:27:093771struct SelectDest {
dan9ed322d2020-04-29 17:41:293772 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
drhfe1c6bb2014-01-22 17:28:353773 int iSDParm; /* A parameter used by the eDest disposal method */
dan9ed322d2020-04-29 17:41:293774 int iSDParm2; /* A second parameter for the eDest disposal method */
drhfe1c6bb2014-01-22 17:28:353775 int iSdst; /* Base register where results are written */
3776 int nSdst; /* Number of registers allocated */
drha8b5c872022-12-14 09:06:453777 char *zAffSdst; /* Affinity used for SRT_Set */
drhfe1c6bb2014-01-22 17:28:353778 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
danielk19776c8c8ce2008-01-02 16:27:093779};
3780
3781/*
mistachkinbfc9b3f2016-02-15 22:01:243782** During code generation of statements that do inserts into AUTOINCREMENT
drh0b9f50d2009-06-23 20:28:533783** tables, the following information is attached to the Table.u.autoInc.p
3784** pointer of each autoincrement table to record some side information that
3785** the code generator needs. We have to keep per-table autoincrement
drh1b325542016-02-03 01:55:443786** information in case inserts are done within triggers. Triggers do not
drh0b9f50d2009-06-23 20:28:533787** normally coordinate their activities, but we do need to coordinate the
3788** loading and saving of autoincrement information.
3789*/
3790struct AutoincInfo {
3791 AutoincInfo *pNext; /* Next info block in a list of them all */
3792 Table *pTab; /* Table this info block refers to */
3793 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
3794 int regCtr; /* Memory register holding the rowid counter */
3795};
3796
3797/*
mistachkinbfc9b3f2016-02-15 22:01:243798** At least one instance of the following structure is created for each
dan2832ad42009-08-31 15:27:273799** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
3800** statement. All such objects are stored in the linked list headed at
3801** Parse.pTriggerPrg and deleted once statement compilation has been
3802** completed.
3803**
3804** A Vdbe sub-program that implements the body and WHEN clause of trigger
3805** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
3806** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
3807** The Parse.pTriggerPrg list never contains two entries with the same
3808** values for both pTrigger and orconf.
dan65a7cd12009-09-01 12:16:013809**
danbb5f1682009-11-27 12:12:343810** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
mistachkinbfc9b3f2016-02-15 22:01:243811** accessed (or set to 0 for triggers fired as a result of INSERT
danbb5f1682009-11-27 12:12:343812** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
3813** a mask of new.* columns used by the program.
dan2832ad42009-08-31 15:27:273814*/
3815struct TriggerPrg {
3816 Trigger *pTrigger; /* Trigger this program was coded from */
dan2832ad42009-08-31 15:27:273817 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
drha4510172012-02-02 15:50:173818 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
3819 int orconf; /* Default ON CONFLICT policy */
3820 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
dan165921a2009-08-28 18:53:453821};
3822
drh64123582011-04-02 20:01:023823/*
3824** The yDbMask datatype for the bitmask of all attached databases.
3825*/
drh01c7dc82011-03-23 18:22:343826#if SQLITE_MAX_ATTACHED>30
drha7ab6d82014-07-21 15:44:393827 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
3828# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
3829# define DbMaskZero(M) memset((M),0,sizeof(M))
3830# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
3831# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
3832# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
drh01c7dc82011-03-23 18:22:343833#else
drh64123582011-04-02 20:01:023834 typedef unsigned int yDbMask;
drha7ab6d82014-07-21 15:44:393835# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
stephandc02d562022-12-23 11:32:063836# define DbMaskZero(M) ((M)=0)
3837# define DbMaskSet(M,I) ((M)|=(((yDbMask)1)<<(I)))
3838# define DbMaskAllZero(M) ((M)==0)
3839# define DbMaskNonZero(M) ((M)!=0)
drh01c7dc82011-03-23 18:22:343840#endif
3841
drhceea3322009-04-23 13:22:423842/*
drhe70d4582022-10-17 14:46:393843** For each index X that has as one of its arguments either an expression
3844** or the name of a virtual generated column, and if X is in scope such that
3845** the value of the expression can simply be read from the index, then
3846** there is an instance of this object on the Parse.pIdxExpr list.
drh4bc1cc12022-10-13 21:08:343847**
drhe70d4582022-10-17 14:46:393848** During code generation, while generating code to evaluate expressions,
3849** this list is consulted and if a matching expression is found, the value
3850** is read from the index rather than being recomputed.
drh4bc1cc12022-10-13 21:08:343851*/
drhe70d4582022-10-17 14:46:393852struct IndexedExpr {
drh4bc1cc12022-10-13 21:08:343853 Expr *pExpr; /* The expression contained in the index */
3854 int iDataCur; /* The data cursor associated with the index */
3855 int iIdxCur; /* The index cursor */
drhe70d4582022-10-17 14:46:393856 int iIdxCol; /* The index column that contains value of pExpr */
drh7a989372022-10-15 11:27:013857 u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
drhdc819022023-03-03 15:12:463858 u8 aff; /* Affinity of the pExpr expression */
drhe70d4582022-10-17 14:46:393859 IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
drh7a2a8ce2022-10-18 20:27:023860#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
3861 const char *zIdxName; /* Name of index, used only for bytecode comments */
3862#endif
drh4bc1cc12022-10-13 21:08:343863};
3864
3865/*
drhcf3c0782021-01-11 20:37:023866** An instance of the ParseCleanup object specifies an operation that
3867** should be performed after parsing to deallocation resources obtained
3868** during the parse and which are no longer needed.
3869*/
3870struct ParseCleanup {
3871 ParseCleanup *pNext; /* Next cleanup task */
3872 void *pPtr; /* Pointer to object to deallocate */
3873 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
3874};
3875
3876/*
drhf57b3392001-10-08 13:22:323877** An SQL parser context. A copy of this structure is passed through
3878** the parser and down into all the parser action routine in order to
3879** carry around information that is global to the entire parse.
drhf1974842004-11-05 03:56:003880**
3881** The structure is divided into two parts. When the parser and code
3882** generate call themselves recursively, the first part of the structure
3883** is constant but the second part is reset at the beginning and end of
3884** each recursion.
danielk1977c00da102006-01-07 13:21:043885**
mistachkinbfc9b3f2016-02-15 22:01:243886** The nTableLock and aTableLock variables are only used if the shared-cache
danielk1977c00da102006-01-07 13:21:043887** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
3888** used to store the set of table-locks required by the statement being
3889** compiled. Function sqlite3TableLock() is used to add entries to the
3890** list.
drh75897232000-05-29 14:26:003891*/
3892struct Parse {
drh9bb575f2004-09-06 17:24:113893 sqlite3 *db; /* The main database structure */
drh75897232000-05-29 14:26:003894 char *zErrMsg; /* An error message */
drh75897232000-05-29 14:26:003895 Vdbe *pVdbe; /* An engine for executing database bytecode */
drha4510172012-02-02 15:50:173896 int rc; /* Return code from execution */
drh84b0f222025-02-07 19:09:203897 LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
drh205f48e2004-11-05 00:43:113898 u8 nested; /* Number of nested calls to the parser/code generator */
drh892d3172008-01-10 03:46:363899 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
drha4510172012-02-02 15:50:173900 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
3901 u8 mayAbort; /* True if statement may throw an ABORT exception */
drhd58d3272013-08-05 22:05:023902 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
drh4a642b62016-02-05 01:55:273903 u8 disableLookaside; /* Number of times lookaside has been disabled */
drh7424aef2022-10-01 13:17:533904 u8 prepFlags; /* SQLITE_PREPARE_* flags */
drh2c31c002022-04-14 16:34:073905 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
dan5525ac12024-06-07 21:00:423906 u8 bHasExists; /* Has a correlated "EXISTS (SELECT ....)" expression */
drh42123a22024-07-05 13:55:593907 u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
drh84b0f222025-02-07 19:09:203908 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
3909 u8 bReturning; /* Coding a RETURNING trigger */
3910 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
3911 u8 disableTriggers; /* True to disable triggers */
drh21d4f5b2021-01-12 15:30:013912#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
3913 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
3914#endif
drha84ead12023-03-17 00:01:323915#ifdef SQLITE_DEBUG
3916 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
drh7fd936e2025-02-07 15:49:213917 u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER)
3918 ** and ALTER TABLE ADD COLUMN. */
drha84ead12023-03-17 00:01:323919#endif
drh03c65172025-02-08 13:34:193920 bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */
3921 bft bHasWith :1; /* True if statement contains WITH */
3922 bft okConstFactor :1; /* OK to factor out constants */
3923 bft checkSchema :1; /* Causes schema cookie check after an error */
drh892d3172008-01-10 03:46:363924 int nRangeReg; /* Size of the temporary register block */
3925 int iRangeReg; /* First register in temporary register block */
drh75897232000-05-29 14:26:003926 int nErr; /* Number of errors seen */
drh832508b2002-03-02 17:04:073927 int nTab; /* Number of previously allocated VDBE cursors */
drh19a775c2000-06-05 18:54:463928 int nMem; /* Number of memory cells used so far */
drhbd573082016-01-01 16:42:093929 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
drh7e8515d2017-12-08 19:37:043930 int iSelfTab; /* Table associated with an index on expr, or negative
drh6e97f8e2017-07-20 13:17:083931 ** of the base register during check-constraint eval */
drhd1d158b2018-12-29 14:23:223932 int nLabel; /* The *negative* of the number of labels used */
drhec4ccdb2018-12-29 02:26:593933 int nLabelAlloc; /* Number of slots in aLabel */
drh7df89c82014-02-04 15:55:253934 int *aLabel; /* Space to hold the labels */
drhf30a9692013-11-15 01:10:183935 ExprList *pConstExpr;/* Constant expressions */
drh03af6d72022-11-21 16:40:123936 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
danbd426422023-09-22 20:21:273937 IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */
drh64123582011-04-02 20:01:023938 yDbMask writeMask; /* Start a write transaction on these databases */
3939 yDbMask cookieMask; /* Bitmask of schema verified databases */
drh84b0f222025-02-07 19:09:203940 int nMaxArg; /* Max args to xUpdate and xFilter vtab methods */
drhfef37762018-07-10 19:48:353941 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
drh80c43862023-08-08 17:36:033942#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3943 u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
3944#endif
danielk1977c00da102006-01-07 13:21:043945#ifndef SQLITE_OMIT_SHARED_CACHE
3946 int nTableLock; /* Number of locks in aTableLock */
3947 TableLock *aTableLock; /* Required table locks for shared-cache mode */
3948#endif
drh0b9f50d2009-06-23 20:28:533949 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
dan65a7cd12009-09-01 12:16:013950 Parse *pToplevel; /* Parse structure for main program (or NULL) */
dan165921a2009-08-28 18:53:453951 Table *pTriggerTab; /* Table triggers are being coded for */
drhb296ab62021-12-31 16:37:463952 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
3953 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
dan165921a2009-08-28 18:53:453954
drh445f3d52016-10-01 21:43:373955 /**************************************************************************
3956 ** Fields above must be initialized to zero. The fields that follow,
3957 ** down to the beginning of the recursive section, do not need to be
3958 ** initialized as they will be set before being used. The boundary is
drh02ceed02018-08-03 23:04:163959 ** determined by offsetof(Parse,aTempReg).
drh445f3d52016-10-01 21:43:373960 **************************************************************************/
3961
drh445f3d52016-10-01 21:43:373962 int aTempReg[8]; /* Holding area for temporary registers */
drhc692df22022-01-24 15:34:553963 Parse *pOuterParse; /* Outer Parse object when nested */
drh445f3d52016-10-01 21:43:373964 Token sNameToken; /* Token with unqualified schema object name */
drhede16902025-02-07 13:37:153965 u32 oldmask; /* Mask of old.* columns referenced */
3966 u32 newmask; /* Mask of new.* columns referenced */
drh7fd936e2025-02-07 15:49:213967 union {
3968 struct { /* These fields available when isCreate is true */
3969 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
3970 int regRowid; /* Register holding rowid of CREATE TABLE entry */
3971 int regRoot; /* Register holding root page for new objects */
3972 Token constraintName; /* Name of the constraint currently being parsed */
3973 } cr;
3974 struct { /* These fields available to all other statements */
3975 Returning *pReturning; /* The RETURNING clause */
3976 } d;
3977 } u1;
drhcd9af602016-09-30 22:24:293978
drh7df89c82014-02-04 15:55:253979 /************************************************************************
3980 ** Above is constant between recursions. Below is reset before and after
3981 ** each recursion. The boundary between these two regions is determined
drh588429a2016-11-14 20:08:003982 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
3983 ** first field in the recursive region.
drh7df89c82014-02-04 15:55:253984 ************************************************************************/
drhf1974842004-11-05 03:56:003985
drh588429a2016-11-14 20:08:003986 Token sLastToken; /* The last token parsed */
drh6d664b42016-01-20 01:48:253987 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
drh7f9c5db2013-10-23 00:32:583988 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
drha4510172012-02-02 15:50:173989 u8 explain; /* True if the EXPLAIN flag is found on the query */
dancf8f2892018-08-09 20:47:013990 u8 eParseMode; /* PARSE_MODE_XXX constant */
drha4510172012-02-02 15:50:173991#ifndef SQLITE_OMIT_VIRTUALTABLE
drha4510172012-02-02 15:50:173992 int nVtabLock; /* Number of virtual tables to lock */
3993#endif
drha4510172012-02-02 15:50:173994 int nHeight; /* Expression tree height of current sub-select */
drhe2ca99c2018-05-02 00:33:433995 int addrExplain; /* Address of current OP_Explain opcode */
drh9bf755c2016-12-23 03:59:313996 VList *pVList; /* Mapping between variable names and numbers */
drha4510172012-02-02 15:50:173997 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
drha4510172012-02-02 15:50:173998 const char *zTail; /* All SQL text past the last semicolon parsed */
3999 Table *pNewTable; /* A table being constructed by CREATE TABLE */
drh885eeb62019-01-09 02:02:244000 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
4001 ** Also used to hold redundant UNIQUE constraints
4002 ** during a RENAME COLUMN */
drhf1974842004-11-05 03:56:004003 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
drhf1974842004-11-05 03:56:004004 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
drhb9bb7c12006-06-11 23:41:554005#ifndef SQLITE_OMIT_VIRTUALTABLE
drha4510172012-02-02 15:50:174006 Token sArg; /* Complete text of a module argument */
4007 Table **apVtabLock; /* Pointer to virtual tables needing locking */
drhb9bb7c12006-06-11 23:41:554008#endif
dan4e9119d2014-01-13 15:12:234009 With *pWith; /* Current WITH clause, or NULL */
dancf8f2892018-08-09 20:47:014010#ifndef SQLITE_OMIT_ALTERTABLE
drh4a2c7472018-08-13 15:09:484011 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
dancf8f2892018-08-09 20:47:014012#endif
drh75897232000-05-29 14:26:004013};
4014
drh074a1312021-10-08 10:25:064015/* Allowed values for Parse.eParseMode
4016*/
dancf8f2892018-08-09 20:47:014017#define PARSE_MODE_NORMAL 0
4018#define PARSE_MODE_DECLARE_VTAB 1
dane6dc1e52019-12-05 14:31:434019#define PARSE_MODE_RENAME 2
4020#define PARSE_MODE_UNMAP 3
dancf8f2892018-08-09 20:47:014021
drha4510172012-02-02 15:50:174022/*
drhcd9af602016-09-30 22:24:294023** Sizes and pointers of various parts of the Parse object.
4024*/
drhc692df22022-01-24 15:34:554025#define PARSE_HDR(X) (((char*)(X))+offsetof(Parse,zErrMsg))
4026#define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColCache*/
drh588429a2016-11-14 20:08:004027#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
drhcd9af602016-09-30 22:24:294028#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
4029#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
4030
4031/*
drha4510172012-02-02 15:50:174032** Return true if currently inside an sqlite3_declare_vtab() call.
4033*/
danielk19777e6ebfb2006-06-12 11:24:374034#ifdef SQLITE_OMIT_VIRTUALTABLE
4035 #define IN_DECLARE_VTAB 0
4036#else
dancf8f2892018-08-09 20:47:014037 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
4038#endif
4039
4040#if defined(SQLITE_OMIT_ALTERTABLE)
danc9461ec2018-08-29 21:00:164041 #define IN_RENAME_OBJECT 0
dancf8f2892018-08-09 20:47:014042#else
dane6dc1e52019-12-05 14:31:434043 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
dancf8f2892018-08-09 20:47:014044#endif
4045
4046#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
4047 #define IN_SPECIAL_PARSE 0
4048#else
4049 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
danielk19777e6ebfb2006-06-12 11:24:374050#endif
4051
danielk1977d99bc932002-05-16 00:13:124052/*
drh85e20962003-04-25 17:52:114053** An instance of the following structure can be declared on a stack and used
4054** to save the Parse.zAuthContext value so that it can be restored later.
4055*/
4056struct AuthContext {
4057 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
4058 Parse *pParse; /* The Parse structure */
4059};
4060
4061/*
drha748fdc2012-03-28 01:34:474062** Bitfield flags for P5 value in various opcodes.
drh49711602016-04-14 16:40:134063**
4064** Value constraints (enforced via assert()):
4065** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
4066** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
4067** OPFLAG_BULKCSR == BTREE_BULKLOAD
4068** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
4069** OPFLAG_FORDELETE == BTREE_FORDELETE
4070** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
4071** OPFLAG_AUXDELETE == BTREE_AUXDELETE
rdcb0c374f2004-02-20 22:53:384072*/
drhe807bdb2016-01-21 17:06:334073#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
4074 /* Also used in P2 (not P5) of OP_Delete */
drh09d00b22018-09-27 20:20:014075#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */
drh97348b32014-09-25 02:44:294076#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
danf91c1312017-01-10 20:04:384077#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
drh3e9ca092009-09-08 01:14:484078#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
4079#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
4080#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
dan46c47d42011-03-01 18:42:074081#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
drha748fdc2012-03-28 01:34:474082#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
4083#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
drh077efc22023-06-22 21:19:374084#define OPFLAG_BYTELENARG 0xc0 /* OP_Column only for octet_length() */
dan428c2182012-08-06 18:50:114085#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
drhe0997b32015-03-20 14:57:504086#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
drh9c0c57a2016-01-21 15:55:374087#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
danfd261ec2015-10-22 20:54:334088#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
drh953f7612012-12-07 22:18:544089#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
danf91c1312017-01-10 20:04:384090#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
drhdef19e32016-01-27 16:26:254091#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
drh41fb3672018-01-12 23:18:384092#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
larrybrbc917382023-06-07 08:40:314093#define OPFLAG_PREFORMAT 0x80 /* OP_Insert uses preformatted cell */
rdcb0c374f2004-02-20 22:53:384094
4095/*
drhc16a5682022-04-06 12:54:414096** Each trigger present in the database schema is stored as an instance of
4097** struct Trigger.
4098**
4099** Pointers to instances of struct Trigger are stored in two ways.
4100** 1. In the "trigHash" hash table (part of the sqlite3* that represents the
4101** database). This allows Trigger structures to be retrieved by name.
4102** 2. All triggers associated with a single table form a linked list, using the
4103** pNext member of struct Trigger. A pointer to the first element of the
4104** linked list is stored as the "pTrigger" member of the associated
4105** struct Table.
4106**
4107** The "step_list" member points to the first element of a linked list
4108** containing the SQL statements specified as the trigger program.
4109*/
danielk1977c3f9bad2002-05-15 08:30:124110struct Trigger {
dan165921a2009-08-28 18:53:454111 char *zName; /* The name of the trigger */
drhdc379452002-05-15 12:45:434112 char *table; /* The table or view to which the trigger applies */
drhf0f258b2003-04-21 18:48:454113 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
drhdca76842004-12-07 14:06:134114 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
drhb8352472021-01-29 19:32:174115 u8 bReturning; /* This trigger implements a RETURNING clause */
shane467bcf32008-11-24 20:01:324116 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
drhdc379452002-05-15 12:45:434117 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
danielk1977d99bc932002-05-16 00:13:124118 the <column-list> is stored here */
danielk1977e501b892006-01-09 06:29:474119 Schema *pSchema; /* Schema containing the trigger */
4120 Schema *pTabSchema; /* Schema containing the table */
drhdc379452002-05-15 12:45:434121 TriggerStep *step_list; /* Link list of trigger program steps */
drhdc379452002-05-15 12:45:434122 Trigger *pNext; /* Next trigger associated with the table */
danielk1977c3f9bad2002-05-15 08:30:124123};
4124
danielk1977d99bc932002-05-16 00:13:124125/*
drhdca76842004-12-07 14:06:134126** A trigger is either a BEFORE or an AFTER trigger. The following constants
mistachkinbfc9b3f2016-02-15 22:01:244127** determine which.
drhdca76842004-12-07 14:06:134128**
4129** If there are multiple triggers, you might of some BEFORE and some AFTER.
4130** In that cases, the constants below can be ORed together.
4131*/
4132#define TRIGGER_BEFORE 1
4133#define TRIGGER_AFTER 2
4134
4135/*
drhc16a5682022-04-06 12:54:414136** An instance of struct TriggerStep is used to store a single SQL statement
4137** that is a part of a trigger-program.
4138**
4139** Instances of struct TriggerStep are stored in a singly linked list (linked
4140** using the "pNext" member) referenced by the "step_list" member of the
4141** associated struct Trigger instance. The first element of the linked list is
4142** the first step of the trigger-program.
4143**
4144** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
4145** "SELECT" statement. The meanings of the other members is determined by the
4146** value of "op" as follows:
4147**
4148** (op == TK_INSERT)
4149** orconf -> stores the ON CONFLICT algorithm
4150** pSelect -> The content to be inserted - either a SELECT statement or
4151** a VALUES clause.
4152** zTarget -> Dequoted name of the table to insert into.
4153** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
4154** statement, then this stores the column-names to be
4155** inserted into.
4156** pUpsert -> The ON CONFLICT clauses for an Upsert
4157**
4158** (op == TK_DELETE)
4159** zTarget -> Dequoted name of the table to delete from.
4160** pWhere -> The WHERE clause of the DELETE statement if one is specified.
4161** Otherwise NULL.
4162**
4163** (op == TK_UPDATE)
4164** zTarget -> Dequoted name of the table to update.
4165** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
4166** Otherwise NULL.
4167** pExprList -> A list of the columns to update and the expressions to update
4168** them to. See sqlite3Update() documentation of "pChanges"
4169** argument.
4170**
4171** (op == TK_SELECT)
4172** pSelect -> The SELECT statement
4173**
4174** (op == TK_RETURNING)
4175** pExprList -> The list of expressions that follow the RETURNING keyword.
4176**
4177*/
danielk1977d99bc932002-05-16 00:13:124178struct TriggerStep {
drhdac9a5f2021-01-29 21:18:464179 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
4180 ** or TK_RETURNING */
drhb1819a02009-07-03 15:37:274181 u8 orconf; /* OE_Rollback etc. */
drha69d9162003-04-17 22:57:534182 Trigger *pTrig; /* The trigger that this step is a part of */
dan46408352015-04-21 16:38:494183 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
4184 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
dane7877b22020-07-14 19:51:014185 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
drhb1819a02009-07-03 15:37:274186 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
drh381bdac2021-02-04 17:29:044187 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
drhb1819a02009-07-03 15:37:274188 IdList *pIdList; /* Column names for INSERT */
drh46d2e5c2018-04-12 13:15:434189 Upsert *pUpsert; /* Upsert clauses on an INSERT */
drhf259df52017-12-27 20:38:354190 char *zSpan; /* Original SQL text of this command */
drh187e4c62006-02-27 22:22:274191 TriggerStep *pNext; /* Next in the link-list */
4192 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
danielk1977d99bc932002-05-16 00:13:124193};
4194
4195/*
drhb8352472021-01-29 19:32:174196** Information about a RETURNING clause
4197*/
4198struct Returning {
4199 Parse *pParse; /* The parse that includes the RETURNING clause */
4200 ExprList *pReturnEL; /* List of expressions to return */
4201 Trigger retTrig; /* The transient trigger that implements RETURNING */
4202 TriggerStep retTStep; /* The trigger step */
drh381bdac2021-02-04 17:29:044203 int iRetCur; /* Transient table holding RETURNING results */
4204 int nRetCol; /* Number of in pReturnEL after expansion */
drh552562c2021-02-04 20:52:204205 int iRetReg; /* Register array for holding a row of RETURNING */
dan94331d42023-10-26 16:05:574206 char zName[40]; /* Name of trigger: "sqlite_returning_%p" */
drhb8352472021-01-29 19:32:174207};
4208
4209/*
stephane9540e22024-06-18 09:58:394210** An object used to accumulate the text of a string where we
drhade86482007-11-28 22:36:404211** do not necessarily know how big the string will be in the end.
4212*/
drh0cdbe1a2018-05-09 13:46:264213struct sqlite3_str {
drh633e6d52008-07-28 19:34:534214 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
drh633e6d52008-07-28 19:34:534215 char *zText; /* The string collected so far */
drhfa385ed2016-01-04 12:07:274216 u32 nAlloc; /* Amount of space allocated in zText */
4217 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
drh3f18e6d2017-08-12 02:01:554218 u32 nChar; /* Length of the string so far */
drh0cdbe1a2018-05-09 13:46:264219 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
drh5f4a6862016-01-30 12:50:254220 u8 printfFlags; /* SQLITE_PRINTF flags below */
drhade86482007-11-28 22:36:404221};
drh5f4a6862016-01-30 12:50:254222#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
4223#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
stephane9540e22024-06-18 09:58:394224#define SQLITE_PRINTF_MALLOCED 0x04 /* True if zText is allocated space */
drh5f4a6862016-01-30 12:50:254225
4226#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
4227
drhf02cc9a2023-07-25 15:08:184228/*
4229** The following object is the header for an "RCStr" or "reference-counted
4230** string". An RCStr is passed around and used like any other char*
4231** that has been dynamically allocated. The important interface
drh44f53b92023-07-26 01:05:084232** differences:
drhf02cc9a2023-07-25 15:08:184233**
drh44f53b92023-07-26 01:05:084234** 1. RCStr strings are reference counted. They are deallocated
4235** when the reference count reaches zero.
drhf02cc9a2023-07-25 15:08:184236**
drh44f53b92023-07-26 01:05:084237** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than
4238** sqlite3_free()
drhf02cc9a2023-07-25 15:08:184239**
drh44f53b92023-07-26 01:05:084240** 3. Make a (read-only) copy of a read-only RCStr string using
4241** sqlite3RCStrRef().
drhca1ce772023-12-01 12:57:124242**
4243** "String" is in the name, but an RCStr object can also be used to hold
4244** binary data.
drhf02cc9a2023-07-25 15:08:184245*/
4246struct RCStr {
drh44f53b92023-07-26 01:05:084247 u64 nRCRef; /* Number of references */
4248 /* Total structure size should be a multiple of 8 bytes for alignment */
drhf02cc9a2023-07-25 15:08:184249};
4250
drhade86482007-11-28 22:36:404251/*
drh234c39d2004-07-24 03:30:474252** A pointer to this structure is used to communicate information
4253** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
4254*/
4255typedef struct {
drh9bb575f2004-09-06 17:24:114256 sqlite3 *db; /* The database being initialized */
drh234c39d2004-07-24 03:30:474257 char **pzErrMsg; /* Error message stored here */
drha4510172012-02-02 15:50:174258 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
drh15ca1df2006-07-26 13:43:304259 int rc; /* Result code stored here */
dan987db762018-08-14 20:18:504260 u32 mInitFlags; /* Flags controlling error messages */
drh6b86e512019-01-05 21:09:374261 u32 nInitRow; /* Number of rows processed */
drh3b3ddba2020-07-22 18:03:564262 Pgno mxPage; /* Maximum page number. 0 for no limit. */
drh234c39d2004-07-24 03:30:474263} InitData;
4264
drhb6c29892004-11-22 19:12:194265/*
dan987db762018-08-14 20:18:504266** Allowed values for mInitFlags
4267*/
drhac894af2021-11-03 15:59:174268#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */
dan6a5a13d2021-02-17 20:08:224269#define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */
4270#define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */
drhac894af2021-11-03 15:59:174271#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */
dan987db762018-08-14 20:18:504272
drhf3c12562021-06-04 13:16:464273/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
4274** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
4275** parameters are for temporary use during development, to help find
larrybrbc917382023-06-07 08:40:314276** optimal values for parameters in the query planner. The should not
drhf3c12562021-06-04 13:16:464277** be used on trunk check-ins. They are a temporary mechanism available
4278** for transient development builds only.
drh2d26cfc2021-06-04 13:40:264279**
4280** Tuning parameters are numbered starting with 1.
drhf3c12562021-06-04 13:16:464281*/
4282#define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
4283#ifdef SQLITE_DEBUG
drh2d26cfc2021-06-04 13:40:264284# define Tuning(X) (sqlite3Config.aTune[(X)-1])
drhf3c12562021-06-04 13:16:464285#else
4286# define Tuning(X) 0
4287#endif
4288
dan987db762018-08-14 20:18:504289/*
drh40257ff2008-06-13 18:24:274290** Structure containing global configuration data for the SQLite library.
drh33589792008-06-18 13:27:464291**
4292** This structure also contains some state information.
drh40257ff2008-06-13 18:24:274293*/
4294struct Sqlite3Config {
drhfec00ea2008-06-14 16:56:214295 int bMemstat; /* True to enable memory status */
drh30842992019-08-12 14:17:434296 u8 bCoreMutex; /* True to enable core mutexing */
4297 u8 bFullMutex; /* True to enable full mutexing */
4298 u8 bOpenUri; /* True to interpret filenames as URIs */
4299 u8 bUseCis; /* Use covering indices for full-scans */
4300 u8 bSmallMalloc; /* Avoid large memory allocations if true */
4301 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
drh7d2eaae2023-12-11 17:03:124302#ifdef SQLITE_DEBUG
drhba550562023-12-11 19:00:444303 u8 bJsonSelfcheck; /* Double-check JSON parsing */
drh7d2eaae2023-12-11 17:03:124304#endif
drh0a687d12008-07-08 14:52:074305 int mxStrlen; /* Maximum string length */
drh09fe6142013-11-29 15:06:274306 int neverCorrupt; /* Database is always well-formed */
drh633e6d52008-07-28 19:34:534307 int szLookaside; /* Default lookaside buffer size */
4308 int nLookaside; /* Default lookaside buffer count */
drh8c71a982016-03-07 17:37:374309 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
drhfec00ea2008-06-14 16:56:214310 sqlite3_mem_methods m; /* Low-level memory allocation interface */
danielk19776d2ab0e2008-06-17 17:21:184311 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
dan22e21ff2011-11-08 20:08:444312 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
drh40257ff2008-06-13 18:24:274313 void *pHeap; /* Heap storage space */
drh33589792008-06-18 13:27:464314 int nHeap; /* Size of pHeap[] */
4315 int mnReq, mxReq; /* Min and max heap requests sizes */
drh9b4c59f2013-04-15 17:03:424316 sqlite3_int64 szMmap; /* mmap() space per open file */
4317 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
drh33589792008-06-18 13:27:464318 void *pPage; /* Page cache memory */
4319 int szPage; /* Size of each page in pPage[] */
4320 int nPage; /* Number of pages in pPage[] */
drh1875f7a2008-12-08 18:19:174321 int mxParserStack; /* maximum depth of the parser stack */
4322 int sharedCacheEnabled; /* true if shared-cache mode enabled */
drh3bd17912015-01-02 15:55:294323 u32 szPma; /* Maximum Sorter PMA size */
drh1875f7a2008-12-08 18:19:174324 /* The above might be initialized to non-zero. The following need to always
4325 ** initially be zero, however. */
danielk197771bc31c2008-06-26 08:29:344326 int isInit; /* True after initialization has finished */
danielk1977502b4e02008-09-02 14:07:244327 int inProgress; /* True while initialization in progress */
dane1ab2192009-08-17 15:16:194328 int isMutexInit; /* True after mutexes are initialized */
danielk197771bc31c2008-06-26 08:29:344329 int isMallocInit; /* True after malloc is initialized */
dane1ab2192009-08-17 15:16:194330 int isPCacheInit; /* True after malloc is initialized */
drh93ed56d2008-08-12 15:21:114331 int nRefInitMutex; /* Number of users of pInitMutex */
drhc007f612014-05-16 14:17:014332 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
drh3f280702010-02-18 18:45:094333 void (*xLog)(void*,int,const char*); /* Function for logging */
4334 void *pLogArg; /* First argument to xLog() */
danac455932012-11-26 19:50:414335#ifdef SQLITE_ENABLE_SQLLOG
4336 void(*xSqllog)(void*,sqlite3*,const char*, int);
4337 void *pSqllogArg;
4338#endif
drh688852a2014-02-17 22:40:434339#ifdef SQLITE_VDBE_COVERAGE
4340 /* The following callback (if not NULL) is invoked on every VDBE branch
4341 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
4342 */
drh7083a482018-07-10 16:04:044343 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
drh688852a2014-02-17 22:40:434344 void *pVdbeBranchArg; /* 1st argument */
4345#endif
drh8d889af2021-05-08 17:18:234346#ifndef SQLITE_OMIT_DESERIALIZE
drh23a88592019-01-31 15:38:534347 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
4348#endif
drhd12602a2016-12-07 15:49:024349#ifndef SQLITE_UNTESTABLE
drhc007f612014-05-16 14:17:014350 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
4351#endif
drh4b42b522024-03-19 13:31:544352#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
4353 u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW
4354 ** feature is disabled. 0 if rowids can
4355 ** occur in views. */
4356#endif
drhc007f612014-05-16 14:17:014357 int bLocaltimeFault; /* True to fail localtime() calls */
drhd7e185c2022-02-10 21:26:534358 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
drh9e5eb9c2016-09-18 16:08:104359 int iOnceResetThreshold; /* When to reset OP_Once counters */
dan2e3a5a82018-04-16 21:12:424360 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
drhade54d62019-08-02 20:45:044361 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
drhf3c12562021-06-04 13:16:464362 /* vvvv--- must be last ---vvv */
4363#ifdef SQLITE_DEBUG
4364 sqlite3_int64 aTune[SQLITE_NTUNE]; /* Tuning parameters */
4365#endif
drh40257ff2008-06-13 18:24:274366};
4367
4368/*
drh09fe6142013-11-29 15:06:274369** This macro is used inside of assert() statements to indicate that
4370** the assert is only valid on a well-formed database. Instead of:
4371**
4372** assert( X );
4373**
4374** One writes:
4375**
drhb2023662013-11-29 15:39:364376** assert( X || CORRUPT_DB );
drh09fe6142013-11-29 15:06:274377**
drhb2023662013-11-29 15:39:364378** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
4379** that the database is definitely corrupt, only that it might be corrupt.
4380** For most test cases, CORRUPT_DB is set to false using a special
4381** sqlite3_test_control(). This enables assert() statements to prove
4382** things that are always true for well-formed databases.
drh09fe6142013-11-29 15:06:274383*/
drhb2023662013-11-29 15:39:364384#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
drh09fe6142013-11-29 15:06:274385
4386/*
drh7d10d5a2008-08-20 16:35:104387** Context pointer passed down through the tree-walk.
4388*/
4389struct Walker {
drh9bfb0242016-01-20 02:21:504390 Parse *pParse; /* Parser context. */
drh7d10d5a2008-08-20 16:35:104391 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
4392 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
danb290f112014-01-17 14:59:274393 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
drh030796d2012-08-23 16:18:104394 int walkerDepth; /* Number of subqueries */
drhdfa15272019-11-06 22:19:074395 u16 eCode; /* A small processing code */
drh038158e2023-06-02 18:05:544396 u16 mWFlags; /* Use-dependent flags */
drh7d10d5a2008-08-20 16:35:104397 union { /* Extra data for callback */
danab31a842017-04-29 20:53:094398 NameContext *pNC; /* Naming context */
4399 int n; /* A counter */
4400 int iCur; /* A cursor number */
4401 SrcList *pSrcList; /* FROM clause */
danab31a842017-04-29 20:53:094402 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
drh90cf38b2021-11-08 23:24:004403 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
danab31a842017-04-29 20:53:094404 int *aiCol; /* array of column indexes */
4405 struct IdxCover *pIdxCover; /* Check for index coverage */
danab31a842017-04-29 20:53:094406 ExprList *pGroupBy; /* GROUP BY clause */
drhcd0abc22018-03-20 18:08:334407 Select *pSelect; /* HAVING to WHERE clause ctx */
dan86fb6e12018-05-16 20:58:074408 struct WindowRewrite *pRewrite; /* Window rewrite context */
drh660ee552018-07-26 21:16:534409 struct WhereConst *pConst; /* WHERE clause constants */
dancf8f2892018-08-09 20:47:014410 struct RenameCtx *pRename; /* RENAME COLUMN context */
drhdfa15272019-11-06 22:19:074411 struct Table *pTab; /* Table of generated column */
drh54cc7662022-10-22 20:13:464412 struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
drh76012942021-02-21 21:04:544413 SrcItem *pSrcItem; /* A single FROM clause item */
drh99a37ca2022-10-24 18:33:504414 DbFixer *pFix; /* See sqlite3FixSelect() */
drhed369172023-04-10 18:44:004415 Mem *aMem; /* See sqlite3BtreeCursorHint() */
drh7d10d5a2008-08-20 16:35:104416 } u;
4417};
4418
danf380c3f2021-01-21 15:40:524419/*
4420** The following structure contains information used by the sqliteFix...
4421** routines as they walk the parse tree to make database references
4422** explicit.
4423*/
4424struct DbFixer {
4425 Parse *pParse; /* The parsing context. Error messages written here */
4426 Walker w; /* Walker object */
4427 Schema *pSchema; /* Fix items to this schema */
4428 u8 bTemp; /* True for TEMP schema entries */
4429 const char *zDb; /* Make sure all objects are contained in this database */
4430 const char *zType; /* Type of the container - used for error messages */
4431 const Token *pName; /* Name of the container - used for error messages */
4432};
4433
drh7d10d5a2008-08-20 16:35:104434/* Forward declarations */
4435int sqlite3WalkExpr(Walker*, Expr*);
drhf82c8cb2023-06-19 23:27:224436int sqlite3WalkExprNN(Walker*, Expr*);
drh7d10d5a2008-08-20 16:35:104437int sqlite3WalkExprList(Walker*, ExprList*);
4438int sqlite3WalkSelect(Walker*, Select*);
4439int sqlite3WalkSelectExpr(Walker*, Select*);
4440int sqlite3WalkSelectFrom(Walker*, Select*);
drh5b88bc42013-12-07 23:35:214441int sqlite3ExprWalkNoop(Walker*, Expr*);
drh979dd1b2017-05-29 14:26:074442int sqlite3SelectWalkNoop(Walker*, Select*);
drh7e6f9802017-09-04 00:33:044443int sqlite3SelectWalkFail(Walker*, Select*);
drhe40cc162020-05-24 03:01:364444int sqlite3WalkerDepthIncrease(Walker*,Select*);
4445void sqlite3WalkerDepthDecrease(Walker*,Select*);
drh5e8e7462021-04-19 19:59:164446void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
drhe40cc162020-05-24 03:01:364447
drh979dd1b2017-05-29 14:26:074448#ifdef SQLITE_DEBUG
4449void sqlite3SelectWalkAssert2(Walker*, Select*);
4450#endif
drh7d10d5a2008-08-20 16:35:104451
danbe120832021-05-17 16:20:414452#ifndef SQLITE_OMIT_CTE
4453void sqlite3SelectPopWith(Walker*, Select*);
4454#else
4455# define sqlite3SelectPopWith 0
4456#endif
4457
drh7d10d5a2008-08-20 16:35:104458/*
4459** Return code from the parse-tree walking primitives and their
4460** callbacks.
4461*/
drh2bf90f12008-12-09 13:04:294462#define WRC_Continue 0 /* Continue down into children */
4463#define WRC_Prune 1 /* Omit children but continue walking siblings */
4464#define WRC_Abort 2 /* Abandon the tree walk */
drh7d10d5a2008-08-20 16:35:104465
4466/*
drhf824b412021-02-20 14:57:164467** A single common table expression
4468*/
4469struct Cte {
4470 char *zName; /* Name of this CTE */
4471 ExprList *pCols; /* List of explicit column names, or NULL */
4472 Select *pSelect; /* The definition of this CTE */
4473 const char *zCteErr; /* Error message for circular references */
drha79e2a22021-02-21 23:44:144474 CteUse *pUse; /* Usage information for this CTE */
drh745912e2021-02-22 03:04:254475 u8 eM10d; /* The MATERIALIZED flag */
drhf824b412021-02-20 14:57:164476};
4477
4478/*
drh745912e2021-02-22 03:04:254479** Allowed values for the materialized flag (eM10d):
4480*/
4481#define M10d_Yes 0 /* AS MATERIALIZED */
4482#define M10d_Any 1 /* Not specified. Query planner's choice */
4483#define M10d_No 2 /* AS NOT MATERIALIZED */
4484
4485/*
drhf824b412021-02-20 14:57:164486** An instance of the With object represents a WITH clause containing
4487** one or more CTEs (common table expressions).
dan7d562db2014-01-11 19:19:364488*/
4489struct With {
drhf824b412021-02-20 14:57:164490 int nCte; /* Number of CTEs in the WITH clause */
dan90bc36f2021-05-20 17:15:064491 int bView; /* Belongs to the outermost Select of a view */
drhf824b412021-02-20 14:57:164492 With *pOuter; /* Containing WITH clause, or NULL */
drhcebf06c2025-03-14 18:10:024493 Cte a[FLEXARRAY]; /* For each CTE in the WITH clause.... */
dan7d562db2014-01-11 19:19:364494};
4495
drhcebf06c2025-03-14 18:10:024496/* The size (in bytes) of a With object that can hold as many
4497** as N different CTEs. */
4498#define SZ_WITH(N) (offsetof(With,a) + (N)*sizeof(Cte))
4499
drha79e2a22021-02-21 23:44:144500/*
4501** The Cte object is not guaranteed to persist for the entire duration
4502** of code generation. (The query flattener or other parser tree
4503** edits might delete it.) The following object records information
4504** about each Common Table Expression that must be preserved for the
4505** duration of the parse.
4506**
4507** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
4508** than sqlite3SelectDelete(), which is what enables them to persist
4509** until the end of code generation.
4510*/
4511struct CteUse {
4512 int nUse; /* Number of users of this CTE */
4513 int addrM9e; /* Start of subroutine to compute materialization */
4514 int regRtn; /* Return address register for addrM9e subroutine */
4515 int iCur; /* Ephemeral table holding the materialization */
4516 LogEst nRowEst; /* Estimated number of rows in the table */
drh745912e2021-02-22 03:04:254517 u8 eM10d; /* The MATERIALIZED flag */
drha79e2a22021-02-21 23:44:144518};
4519
4520
drh10deb352023-08-30 15:20:154521/* Client data associated with sqlite3_set_clientdata() and
4522** sqlite3_get_clientdata().
4523*/
4524struct DbClientData {
4525 DbClientData *pNext; /* Next in a linked list */
4526 void *pData; /* The data */
4527 void (*xDestructor)(void*); /* Destructor. Might be NULL */
drhcebf06c2025-03-14 18:10:024528 char zName[FLEXARRAY]; /* Name of this client data. MUST BE LAST */
drh10deb352023-08-30 15:20:154529};
4530
drhcebf06c2025-03-14 18:10:024531/* The size (in bytes) of a DbClientData object that can has a name
4532** that is N bytes long, including the zero-terminator. */
4533#define SZ_DBCLIENTDATA(N) (offsetof(DbClientData,zName)+(N))
4534
drh4fa4a542014-09-30 12:33:334535#ifdef SQLITE_DEBUG
4536/*
4537** An instance of the TreeView object is used for printing the content of
4538** data structures on sqlite3DebugPrintf() using a tree-like view.
4539*/
4540struct TreeView {
4541 int iLevel; /* Which level of the tree we are on */
drhb08cd3f2014-09-30 19:04:414542 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
drh4fa4a542014-09-30 12:33:334543};
4544#endif /* SQLITE_DEBUG */
4545
dan9a947222018-06-14 19:06:364546/*
dandf9d3242019-07-13 16:39:384547** This object is used in various ways, most (but not all) related to window
4548** functions.
drha1fd4b52018-07-10 06:32:534549**
4550** (1) A single instance of this structure is attached to the
dandf9d3242019-07-13 16:39:384551** the Expr.y.pWin field for each window function in an expression tree.
drha1fd4b52018-07-10 06:32:534552** This object holds the information contained in the OVER clause,
4553** plus additional fields used during code generation.
4554**
4555** (2) All window functions in a single SELECT form a linked-list
4556** attached to Select.pWin. The Window.pFunc and Window.pExpr
4557** fields point back to the expression that is the window function.
4558**
4559** (3) The terms of the WINDOW clause of a SELECT are instances of this
4560** object on a linked list attached to Select.pWinDefn.
4561**
dandf9d3242019-07-13 16:39:384562** (4) For an aggregate function with a FILTER clause, an instance
4563** of this object is stored in Expr.y.pWin with eFrmType set to
4564** TK_FILTER. In this case the only field used is Window.pFilter.
4565**
drha1fd4b52018-07-10 06:32:534566** The uses (1) and (2) are really the same Window object that just happens
drhfc15f4c2019-03-28 13:03:414567** to be accessible in two different ways. Use case (3) are separate objects.
dan9a947222018-06-14 19:06:364568*/
dan86fb6e12018-05-16 20:58:074569struct Window {
dane3bf6322018-06-08 20:58:274570 char *zName; /* Name of window (may be NULL) */
dane7c9ca42019-02-16 17:27:514571 char *zBase; /* Name of base window for chaining (may be NULL) */
dane3bf6322018-06-08 20:58:274572 ExprList *pPartition; /* PARTITION BY clause */
4573 ExprList *pOrderBy; /* ORDER BY clause */
drhfc15f4c2019-03-28 13:03:414574 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
dan86fb6e12018-05-16 20:58:074575 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
4576 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
dane7c9ca42019-02-16 17:27:514577 u8 bImplicitFrame; /* True if frame was implicitly specified */
drhfc15f4c2019-03-28 13:03:414578 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
dan86fb6e12018-05-16 20:58:074579 Expr *pStart; /* Expression for "<expr> PRECEDING" */
4580 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
dan75b08212019-07-22 16:20:034581 Window **ppThis; /* Pointer to this object in Select.pWin list */
dan86fb6e12018-05-16 20:58:074582 Window *pNextWin; /* Next window function belonging to this SELECT */
drha1fd4b52018-07-10 06:32:534583 Expr *pFilter; /* The FILTER expression */
drh105dcaa2022-03-10 16:01:144584 FuncDef *pWFunc; /* The function */
drhb0225bc2018-07-10 20:50:274585 int iEphCsr; /* Partition buffer or Peer buffer */
drhd44c6172019-09-14 16:21:024586 int regAccum; /* Accumulator */
4587 int regResult; /* Interim result */
danc9a86682018-05-30 20:44:584588 int csrApp; /* Function cursor (used by min/max) */
4589 int regApp; /* Function register (also used by min/max) */
danb6f2dea2019-03-13 17:20:274590 int regPart; /* Array of registers for PARTITION BY values */
dan86fb6e12018-05-16 20:58:074591 Expr *pOwner; /* Expression object this window is attached to */
4592 int nBufferCol; /* Number of columns in buffer table */
4593 int iArgCol; /* Offset of first argument for this function */
danbf845152019-03-16 10:15:244594 int regOne; /* Register containing constant value 1 */
dana0f6b832019-03-14 16:36:204595 int regStartRowid;
4596 int regEndRowid;
drhd44c6172019-09-14 16:21:024597 u8 bExprArgs; /* Defer evaluation of window function arguments
4598 ** due to the SQLITE_SUBTYPE flag */
dan86fb6e12018-05-16 20:58:074599};
4600
dan815e0552024-03-11 17:27:194601Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow);
4602void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal);
4603
dan67a9b8e2018-06-22 20:51:354604#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:074605void sqlite3WindowDelete(sqlite3*, Window*);
drhe2094572019-07-22 19:01:384606void sqlite3WindowUnlinkFromSelect(Window*);
dane3bf6322018-06-08 20:58:274607void sqlite3WindowListDelete(sqlite3 *db, Window *p);
dand35300f2019-03-14 20:53:214608Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
dan86fb6e12018-05-16 20:58:074609void sqlite3WindowAttach(Parse*, Expr*, Window*);
dana3fcc002019-08-15 13:53:224610void sqlite3WindowLink(Select *pSel, Window *pWin);
drh1580d502021-09-25 17:07:574611int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
dan4ea562e2020-01-01 20:17:154612void sqlite3WindowCodeInit(Parse*, Select*);
dandacf1de2018-06-08 16:11:554613void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
dandfa552f2018-06-02 21:04:284614int sqlite3WindowRewrite(Parse*, Select*);
dane3bf6322018-06-08 20:58:274615void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
dan2a11bb22018-06-11 20:50:254616Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
danc95f38d2018-06-18 20:34:434617Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
dan9a947222018-06-14 19:06:364618void sqlite3WindowFunctions(void);
dane7c9ca42019-02-16 17:27:514619void sqlite3WindowChain(Parse*, Window*, Window*);
4620Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
dan67a9b8e2018-06-22 20:51:354621#else
4622# define sqlite3WindowDelete(a,b)
4623# define sqlite3WindowFunctions()
4624# define sqlite3WindowAttach(a,b,c)
4625#endif
dan86fb6e12018-05-16 20:58:074626
dan7d562db2014-01-11 19:19:364627/*
drh66150952007-07-23 19:12:414628** Assuming zIn points to the first byte of a UTF-8 character,
4629** advance zIn to point to the first byte of the next UTF-8 character.
drh4a919112007-05-15 11:55:094630*/
drh4a919112007-05-15 11:55:094631#define SQLITE_SKIP_UTF8(zIn) { \
4632 if( (*(zIn++))>=0xc0 ){ \
4633 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
4634 } \
4635}
4636
drh4a919112007-05-15 11:55:094637/*
drh9978c972010-02-23 17:36:324638** The SQLITE_*_BKPT macros are substitutes for the error codes with
4639** the same name but without the _BKPT suffix. These macros invoke
4640** routines that report the line-number on which the error originated
4641** using sqlite3_log(). The routines also provide a convenient place
4642** to set a debugger breakpoint.
drh49285702005-09-17 15:20:264643*/
daneebf2f52017-11-18 17:30:084644int sqlite3ReportError(int iErr, int lineno, const char *zType);
drh9978c972010-02-23 17:36:324645int sqlite3CorruptError(int);
4646int sqlite3MisuseError(int);
4647int sqlite3CantopenError(int);
4648#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
4649#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
4650#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
drh32c49902016-02-15 18:15:154651#ifdef SQLITE_DEBUG
4652 int sqlite3NomemError(int);
4653 int sqlite3IoerrnomemError(int);
4654# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
4655# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
4656#else
4657# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
4658# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
dan3cdc8202020-02-04 20:01:444659#endif
4660#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
4661 int sqlite3CorruptPgnoError(int,Pgno);
4662# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
4663#else
drhcc97ca42017-06-07 22:32:594664# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
drh32c49902016-02-15 18:15:154665#endif
drh9978c972010-02-23 17:36:324666
drh4553f6e2016-02-11 22:41:044667/*
4668** FTS3 and FTS4 both require virtual table support
4669*/
4670#if defined(SQLITE_OMIT_VIRTUALTABLE)
4671# undef SQLITE_ENABLE_FTS3
4672# undef SQLITE_ENABLE_FTS4
4673#endif
drh49285702005-09-17 15:20:264674
4675/*
drhb4a1fed2010-02-03 19:55:134676** FTS4 is really an extension for FTS3. It is enabled using the
peter.d.reid60ec9142014-09-06 16:39:464677** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
4678** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
drhb4a1fed2010-02-03 19:55:134679*/
4680#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
drh0ede9eb2015-01-10 16:49:234681# define SQLITE_ENABLE_FTS3 1
drhb4a1fed2010-02-03 19:55:134682#endif
4683
4684/*
danielk197778ca0e72009-01-20 16:53:394685** The following macros mimic the standard library functions toupper(),
4686** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
4687** sqlite versions only work for ASCII characters, regardless of locale.
4688*/
4689#ifdef SQLITE_ASCII
4690# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
4691# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
drhdc86e2b2009-01-24 11:30:424692# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
4693# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
danielk197778ca0e72009-01-20 16:53:394694# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
4695# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
4696# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
drh244b9d62016-04-11 19:01:084697# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
drhdae7ae32023-04-30 20:37:494698# define sqlite3JsonId1(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x42)
4699# define sqlite3JsonId2(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x46)
danielk197778ca0e72009-01-20 16:53:394700#else
danielk197778ca0e72009-01-20 16:53:394701# define sqlite3Toupper(x) toupper((unsigned char)(x))
4702# define sqlite3Isspace(x) isspace((unsigned char)(x))
4703# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
drhdc86e2b2009-01-24 11:30:424704# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
danielk197778ca0e72009-01-20 16:53:394705# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
4706# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
4707# define sqlite3Tolower(x) tolower((unsigned char)(x))
drh244b9d62016-04-11 19:01:084708# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
drhdae7ae32023-04-30 20:37:494709# define sqlite3JsonId1(x) (sqlite3IsIdChar(x)&&(x)<'0')
4710# define sqlite3JsonId2(x) sqlite3IsIdChar(x)
danielk197778ca0e72009-01-20 16:53:394711#endif
drh97348b32014-09-25 02:44:294712int sqlite3IsIdChar(u8);
danielk197778ca0e72009-01-20 16:53:394713
4714/*
drh75897232000-05-29 14:26:004715** Internal function prototypes
4716*/
drh80738d92016-02-15 00:34:164717int sqlite3StrICmp(const char*,const char*);
drhea678832008-12-10 19:26:224718int sqlite3Strlen30(const char*);
drh7301e772018-10-31 20:52:004719#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
drhd7564862016-03-22 20:05:094720char *sqlite3ColumnType(Column*,char*);
danielk1977ee0484c2009-07-28 16:44:264721#define sqlite3StrNICmp sqlite3_strnicmp
danielk19772e588c72005-12-09 14:25:084722
drh40257ff2008-06-13 18:24:274723int sqlite3MallocInit(void);
drhfec00ea2008-06-14 16:56:214724void sqlite3MallocEnd(void);
drhda4ca9d2014-09-09 17:27:354725void *sqlite3Malloc(u64);
4726void *sqlite3MallocZero(u64);
4727void *sqlite3DbMallocZero(sqlite3*, u64);
4728void *sqlite3DbMallocRaw(sqlite3*, u64);
drh575fad62016-02-05 13:38:364729void *sqlite3DbMallocRawNN(sqlite3*, u64);
drh17435752007-08-16 04:30:384730char *sqlite3DbStrDup(sqlite3*,const char*);
drhda4ca9d2014-09-09 17:27:354731char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
drh9b2e0432017-12-27 19:43:224732char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
drhda4ca9d2014-09-09 17:27:354733void *sqlite3Realloc(void*, u64);
4734void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
4735void *sqlite3DbRealloc(sqlite3 *, void *, u64);
drh633e6d52008-07-28 19:34:534736void sqlite3DbFree(sqlite3*, void*);
drhdbd6a7d2017-04-05 12:39:494737void sqlite3DbFreeNN(sqlite3*, void*);
drh41ce47c2022-08-22 02:00:264738void sqlite3DbNNFreeNN(sqlite3*, void*);
drhb6dad522021-09-24 16:14:474739int sqlite3MallocSize(const void*);
4740int sqlite3DbMallocSize(sqlite3*, const void*);
drhfacf0302008-06-17 15:12:004741void *sqlite3PageMalloc(int);
4742void sqlite3PageFree(void*);
drhfec00ea2008-06-14 16:56:214743void sqlite3MemSetDefault(void);
drhd12602a2016-12-07 15:49:024744#ifndef SQLITE_UNTESTABLE
danielk1977171bfed2008-06-23 09:50:504745void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
drhf5ed7ad2015-06-15 14:43:254746#endif
drh50d1b5f2010-08-27 12:21:064747int sqlite3HeapNearlyFull(void);
danielk19772e588c72005-12-09 14:25:084748
drhe7b347072009-06-01 18:18:204749/*
4750** On systems with ample stack space and that support alloca(), make
4751** use of alloca() to obtain space for large automatic objects. By default,
4752** obtain space from malloc().
4753**
4754** The alloca() routine never returns NULL. This will cause code paths
4755** that deal with sqlite3StackAlloc() failures to be unreachable.
4756*/
4757#ifdef SQLITE_USE_ALLOCA
4758# define sqlite3StackAllocRaw(D,N) alloca(N)
drhce4b0fd2022-10-17 10:15:414759# define sqlite3StackAllocRawNN(D,N) alloca(N)
mistachkinbfc9b3f2016-02-15 22:01:244760# define sqlite3StackFree(D,P)
drhce4b0fd2022-10-17 10:15:414761# define sqlite3StackFreeNN(D,P)
drhe7b347072009-06-01 18:18:204762#else
4763# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
drhce4b0fd2022-10-17 10:15:414764# define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
drhe7b347072009-06-01 18:18:204765# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
drhce4b0fd2022-10-17 10:15:414766# define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P)
drhe7b347072009-06-01 18:18:204767#endif
4768
drh5d513ba2016-07-25 11:57:214769/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
4770** are, disable MEMSYS3
4771*/
danielk1977f3d3c272008-11-19 16:52:444772#ifdef SQLITE_ENABLE_MEMSYS5
4773const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
drh5d513ba2016-07-25 11:57:214774#undef SQLITE_ENABLE_MEMSYS3
4775#endif
4776#ifdef SQLITE_ENABLE_MEMSYS3
4777const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
danielk1977f3d3c272008-11-19 16:52:444778#endif
4779
4780
drh18472fa2008-10-07 15:25:484781#ifndef SQLITE_MUTEX_OMIT
dan558814f2010-06-02 05:53:534782 sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
4783 sqlite3_mutex_methods const *sqlite3NoopMutex(void);
drh65bbf292008-06-19 01:03:174784 sqlite3_mutex *sqlite3MutexAlloc(int);
4785 int sqlite3MutexInit(void);
4786 int sqlite3MutexEnd(void);
4787#endif
drh0e8729d2015-09-10 04:17:064788#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
drh6081c1d2015-09-06 02:51:044789 void sqlite3MemoryBarrier(void);
drh0e8729d2015-09-10 04:17:064790#else
mistachkin04abf082015-09-12 18:57:454791# define sqlite3MemoryBarrier()
drhf7141992008-06-19 00:16:084792#endif
4793
drhaf89fe62015-03-23 17:25:184794sqlite3_int64 sqlite3StatusValue(int);
4795void sqlite3StatusUp(int, int);
4796void sqlite3StatusDown(int, int);
drhb02392e2015-10-15 15:28:564797void sqlite3StatusHighwater(int, int);
drh52fb8e12017-08-29 20:21:124798int sqlite3LookasideUsed(sqlite3*,int*);
drhf7141992008-06-19 00:16:084799
drhaf89fe62015-03-23 17:25:184800/* Access to mutexes used by sqlite3_status() */
4801sqlite3_mutex *sqlite3Pcache1Mutex(void);
4802sqlite3_mutex *sqlite3MallocMutex(void);
4803
dan61f8e862017-11-25 21:09:294804#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
dan8385bec2017-11-25 17:51:014805void sqlite3MutexWarnOnContention(sqlite3_mutex*);
4806#else
4807# define sqlite3MutexWarnOnContention(x)
4808#endif
4809
drh85c8f292010-01-13 17:39:534810#ifndef SQLITE_OMIT_FLOATING_POINT
drh05921222019-05-30 00:46:374811# define EXP754 (((u64)0x7ff)<<52)
4812# define MAN754 ((((u64)1)<<52)-1)
4813# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
drh5ed044e2024-03-19 10:16:174814# define IsOvfl(X) (((X)&EXP754)==EXP754)
drh85c8f292010-01-13 17:39:534815 int sqlite3IsNaN(double);
drh5ed044e2024-03-19 10:16:174816 int sqlite3IsOverflow(double);
drh85c8f292010-01-13 17:39:534817#else
drh5ed044e2024-03-19 10:16:174818# define IsNaN(X) 0
4819# define sqlite3IsNaN(X) 0
4820# define sqlite3IsOVerflow(X) 0
drh85c8f292010-01-13 17:39:534821#endif
drh0de3ae92008-04-28 16:55:264822
drha5c14162013-12-17 15:03:064823/*
4824** An instance of the following structure holds information about SQL
4825** functions arguments that are the parameters to the printf() function.
4826*/
4827struct PrintfArguments {
4828 int nArg; /* Total number of arguments */
4829 int nUsed; /* Number of arguments used so far */
4830 sqlite3_value **apArg; /* The argument values */
4831};
4832
drha1b0ff12023-06-30 18:35:434833/*
4834** An instance of this object receives the decoding of a floating point
4835** value into an approximate decimal representation.
4836*/
4837struct FpDecode {
4838 char sign; /* '+' or '-' */
drh9ee94442023-07-01 15:23:244839 char isSpecial; /* 1: Infinity 2: NaN */
drha1b0ff12023-06-30 18:35:434840 int n; /* Significant digits in the decode */
4841 int iDP; /* Location of the decimal point */
drh50ba4e32023-07-07 18:49:084842 char *z; /* Start of significant digits */
4843 char zBuf[24]; /* Storage for significant digits */
drha1b0ff12023-06-30 18:35:434844};
4845
drh17c20bb2023-07-01 17:56:004846void sqlite3FpDecode(FpDecode*,double,int,int);
drh17435752007-08-16 04:30:384847char *sqlite3MPrintf(sqlite3*,const char*, ...);
4848char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
mistachkin02b0e262015-04-16 03:37:194849#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
drh87cc3b32007-05-08 21:45:274850 void sqlite3DebugPrintf(const char*, ...);
drhd919fe12007-12-11 19:34:444851#endif
4852#if defined(SQLITE_TEST)
drhe8f52c52008-07-12 14:52:204853 void *sqlite3TestTextToPtr(const char*);
drh87cc3b32007-05-08 21:45:274854#endif
drh7e02e5e2011-12-06 19:44:514855
drh4fa4a542014-09-30 12:33:334856#if defined(SQLITE_DEBUG)
drh2a7dcbf2022-04-06 15:41:534857 void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...);
drh4fa4a542014-09-30 12:33:334858 void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
drhdb97e562016-08-18 17:55:574859 void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
drh4fa4a542014-09-30 12:33:334860 void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
drh7d2c1d22022-04-06 00:29:214861 void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*);
4862 void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*);
drha087eb82022-04-28 18:17:514863 void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
drh145d0a32018-11-08 22:53:064864 void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
drh4fa4a542014-09-30 12:33:334865 void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
drh2476a6f2015-11-07 15:19:594866 void sqlite3TreeViewWith(TreeView*, const With*, u8);
drh7d2c1d22022-04-06 00:29:214867 void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8);
drhf1ab6422022-07-11 18:26:144868#if TREETRACE_ENABLED
drh2a7dcbf2022-04-06 15:41:534869 void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*,
4870 const ExprList*,const Expr*, const Trigger*);
4871 void sqlite3TreeViewInsert(const With*, const SrcList*,
drhc2d0df92022-04-06 18:30:174872 const IdList*, const Select*, const ExprList*,
4873 int, const Upsert*, const Trigger*);
drh2a7dcbf2022-04-06 15:41:534874 void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*,
4875 const Expr*, int, const ExprList*, const Expr*,
4876 const Upsert*, const Trigger*);
drhf1ab6422022-07-11 18:26:144877#endif
drh2a7dcbf2022-04-06 15:41:534878#ifndef SQLITE_OMIT_TRIGGER
4879 void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8);
4880 void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8);
4881#endif
drha1fd4b52018-07-10 06:32:534882#ifndef SQLITE_OMIT_WINDOWFUNC
4883 void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
4884 void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
4885#endif
drh8f1eb6f2022-04-06 12:25:044886 void sqlite3ShowExpr(const Expr*);
4887 void sqlite3ShowExprList(const ExprList*);
4888 void sqlite3ShowIdList(const IdList*);
4889 void sqlite3ShowSrcList(const SrcList*);
4890 void sqlite3ShowSelect(const Select*);
4891 void sqlite3ShowWith(const With*);
4892 void sqlite3ShowUpsert(const Upsert*);
drh2a7dcbf2022-04-06 15:41:534893#ifndef SQLITE_OMIT_TRIGGER
4894 void sqlite3ShowTriggerStep(const TriggerStep*);
4895 void sqlite3ShowTriggerStepList(const TriggerStep*);
4896 void sqlite3ShowTrigger(const Trigger*);
4897 void sqlite3ShowTriggerList(const Trigger*);
4898#endif
drh8f1eb6f2022-04-06 12:25:044899#ifndef SQLITE_OMIT_WINDOWFUNC
4900 void sqlite3ShowWindow(const Window*);
4901 void sqlite3ShowWinFunc(const Window*);
drh7e02e5e2011-12-06 19:44:514902#endif
drh90ba0d42025-06-10 16:02:294903 void sqlite3ShowBitvec(Bitvec*);
drh8f1eb6f2022-04-06 12:25:044904#endif
drh7e02e5e2011-12-06 19:44:514905
drh22c17b82015-05-15 04:13:154906void sqlite3SetString(char **, sqlite3*, const char*);
drhf84cbd12023-01-12 13:25:484907void sqlite3ProgressCheck(Parse*);
danielk19774adee202004-05-08 08:23:194908void sqlite3ErrorMsg(Parse*, const char*, ...);
drhc3dcdba2019-04-09 21:32:464909int sqlite3ErrorToParser(sqlite3*,int);
drh244b9d62016-04-11 19:01:084910void sqlite3Dequote(char*);
drh51d35b02019-01-11 13:32:234911void sqlite3DequoteExpr(Expr*);
drh77441fa2021-07-30 18:39:594912void sqlite3DequoteToken(Token*);
dan406eb5a2024-01-23 11:20:584913void sqlite3DequoteNumber(Parse*, Expr*);
drh40aced52016-01-22 17:48:094914void sqlite3TokenInit(Token*,char*);
drh2646da72005-12-09 20:02:054915int sqlite3KeywordCode(const unsigned char*, int);
drh54bc6382021-12-31 19:20:424916int sqlite3RunParser(Parse*, const char*);
drh80242052004-06-09 00:48:124917void sqlite3FinishCoding(Parse*);
drh892d3172008-01-10 03:46:364918int sqlite3GetTempReg(Parse*);
4919void sqlite3ReleaseTempReg(Parse*,int);
4920int sqlite3GetTempRange(Parse*,int);
4921void sqlite3ReleaseTempRange(Parse*,int,int);
drhcdc69552011-12-06 13:24:594922void sqlite3ClearTempRegCache(Parse*);
drhaa9192e2023-03-26 16:36:274923void sqlite3TouchRegister(Parse*,int);
drh54b81e32023-04-01 15:51:214924#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
drhaa9192e2023-03-26 16:36:274925int sqlite3FirstAvailableRegister(Parse*,int);
drh54b81e32023-04-01 15:51:214926#endif
drhbb9b5f22016-03-19 00:35:024927#ifdef SQLITE_DEBUG
4928int sqlite3NoTempsInRange(Parse*,int,int);
4929#endif
drhb7916a72009-05-27 10:31:294930Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
4931Expr *sqlite3Expr(sqlite3*,int,const char*);
4932void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
drhabfd35e2016-12-06 22:47:234933Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
drh08de4f72016-04-11 01:06:474934void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
drhd5c851c2019-04-19 13:38:344935Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
drh17180fc2019-04-19 17:26:194936Expr *sqlite3ExprSimplifiedAndOr(Expr*);
drhb6dad522021-09-24 16:14:474937Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
drhf8202f12023-10-18 13:18:524938void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*);
drh20b95f82023-10-18 22:03:484939void sqlite3ExprOrderByAggregateError(Parse*,Expr*);
drhb6dad522021-09-24 16:14:474940void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
drhde25a882016-10-03 15:28:244941void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
drh633e6d52008-07-28 19:34:534942void sqlite3ExprDelete(sqlite3*, Expr*);
drh82fc1b62023-12-06 18:25:414943void sqlite3ExprDeleteGeneric(sqlite3*,void*);
drh32542062024-05-10 18:10:344944int sqlite3ExprDeferredDelete(Parse*, Expr*);
drh8e34e402019-06-11 10:43:564945void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
drhb7916a72009-05-27 10:31:294946ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
drha1251bc2016-08-20 00:51:374947ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
dan74777f92021-07-07 13:53:554948Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
dan6e118922019-08-12 16:36:384949void sqlite3ExprListSetSortOrder(ExprList*,int,int);
drhb6dad522021-09-24 16:14:474950void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
drh1be266b2017-12-24 00:18:474951void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
drh633e6d52008-07-28 19:34:534952void sqlite3ExprListDelete(sqlite3*, ExprList*);
drh82fc1b62023-12-06 18:25:414953void sqlite3ExprListDeleteGeneric(sqlite3*,void*);
drh2308ed32015-02-09 16:09:344954u32 sqlite3ExprListFlags(const ExprList*);
drh8d406732019-01-30 18:33:334955int sqlite3IndexHasDuplicateRootPage(Index*);
drh9bb575f2004-09-06 17:24:114956int sqlite3Init(sqlite3*, char**);
drh234c39d2004-07-24 03:30:474957int sqlite3InitCallback(void*, int, char**, char**);
dan987db762018-08-14 20:18:504958int sqlite3InitOne(sqlite3*, int, char**, u32);
danielk197791cf71b2004-06-26 06:37:064959void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
drh2fcc1592016-12-15 20:59:034960#ifndef SQLITE_OMIT_VIRTUALTABLE
4961Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
4962#endif
drh81028a42012-05-15 18:28:274963void sqlite3ResetAllSchemasOfConnection(sqlite3*);
4964void sqlite3ResetOneSchema(sqlite3*,int);
4965void sqlite3CollapseDatabaseArray(sqlite3*);
drh9bb575f2004-09-06 17:24:114966void sqlite3CommitInternalChanges(sqlite3*);
drh79cf2b72021-07-31 20:30:414967void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
4968Expr *sqlite3ColumnExpr(Table*,Column*);
drh65b40092021-08-05 15:27:194969void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
4970const char *sqlite3ColumnColl(Column*);
drh51be3872015-08-19 02:32:254971void sqlite3DeleteColumnNames(sqlite3*,Table*);
drh90881862021-05-19 12:17:034972void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
drh8981b902015-08-24 17:42:494973int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
drh9e660872022-12-13 15:54:434974void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
drh81506b82019-08-05 19:32:064975Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
drh346a70c2020-06-15 20:27:354976void sqlite3OpenSchemaTable(Parse *, int);
drh44156282013-10-23 22:23:034977Index *sqlite3PrimaryKeyIndex(Table*);
drhcc803b22025-02-21 20:35:374978int sqlite3TableColumnToIndex(Index*, int);
drh81f7b372019-10-16 12:18:594979#ifdef SQLITE_OMIT_GENERATED_COLUMNS
drhb9bcf7c2019-10-19 13:29:104980# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
4981# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
drh81f7b372019-10-16 12:18:594982#else
drhb9bcf7c2019-10-19 13:29:104983 i16 sqlite3TableColumnToStorage(Table*, i16);
4984 i16 sqlite3StorageColumnToTable(Table*, i16);
drh81f7b372019-10-16 12:18:594985#endif
danielk1977f1a381e2006-06-16 08:01:024986void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
drhe6110502015-12-31 15:34:034987#if SQLITE_ENABLE_HIDDEN_COLUMNS
4988 void sqlite3ColumnPropertiesFromName(Table*, Column*);
4989#else
4990# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
4991#endif
drh77441fa2021-07-30 18:39:594992void sqlite3AddColumn(Parse*,Token,Token);
danielk19774adee202004-05-08 08:23:194993void sqlite3AddNotNull(Parse*, int);
drhfdd6e852005-12-16 01:06:164994void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
drh92e21ef2020-08-27 18:36:304995void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
drh1be266b2017-12-24 00:18:474996void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
danielk197739002502007-11-12 09:50:264997void sqlite3AddCollateType(Parse*, Token*);
drh81f7b372019-10-16 12:18:594998void sqlite3AddGenerated(Parse*,Expr*,Token*);
drh44183f82021-08-18 13:13:584999void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
drh2053f312021-01-12 20:16:315000void sqlite3AddReturning(Parse*,ExprList*);
drh522c26f2011-05-07 14:40:295001int sqlite3ParseUri(const char*,const char*,unsigned int*,
5002 sqlite3_vfs**,char**,char **);
drhb48c0d52020-02-07 01:12:535003#define sqlite3CodecQueryParameters(A,B,C) 0
drh421377e2012-03-15 21:28:545004Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
drhb7f91642004-10-31 02:22:475005
drhd12602a2016-12-07 15:49:025006#ifdef SQLITE_UNTESTABLE
drhc007f612014-05-16 14:17:015007# define sqlite3FaultSim(X) SQLITE_OK
5008#else
5009 int sqlite3FaultSim(int);
5010#endif
5011
drhf5e7bb52008-02-18 14:47:335012Bitvec *sqlite3BitvecCreate(u32);
5013int sqlite3BitvecTest(Bitvec*, u32);
drh82ef8772015-06-29 14:11:505014int sqlite3BitvecTestNotNull(Bitvec*, u32);
drhf5e7bb52008-02-18 14:47:335015int sqlite3BitvecSet(Bitvec*, u32);
drhe98c9042009-06-02 21:31:385016void sqlite3BitvecClear(Bitvec*, u32, void*);
drhf5e7bb52008-02-18 14:47:335017void sqlite3BitvecDestroy(Bitvec*);
danielk1977bea2a942009-01-20 17:06:275018u32 sqlite3BitvecSize(Bitvec*);
drhd12602a2016-12-07 15:49:025019#ifndef SQLITE_UNTESTABLE
drh3088d592008-03-21 16:45:475020int sqlite3BitvecBuiltinTest(int,int*);
drhf5ed7ad2015-06-15 14:43:255021#endif
drhf5e7bb52008-02-18 14:47:335022
drh9d67afc2018-08-29 20:24:035023RowSet *sqlite3RowSetInit(sqlite3*);
5024void sqlite3RowSetDelete(void*);
5025void sqlite3RowSetClear(void*);
drh3d4501e2008-12-04 20:40:105026void sqlite3RowSetInsert(RowSet*, i64);
drhd83cad22014-04-10 02:24:485027int sqlite3RowSetTest(RowSet*, int iBatch, i64);
drh3d4501e2008-12-04 20:40:105028int sqlite3RowSetNext(RowSet*, i64*);
5029
drh8981b902015-08-24 17:42:495030void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
danielk1977fe3fcbe22006-06-12 12:08:455031
5032#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
drhb7f91642004-10-31 02:22:475033 int sqlite3ViewGetColumnNames(Parse*,Table*);
5034#else
5035# define sqlite3ViewGetColumnNames(A,B) 0
5036#endif
5037
drha7ab6d82014-07-21 15:44:395038#if SQLITE_MAX_ATTACHED>30
5039 int sqlite3DbMaskAllZero(yDbMask);
5040#endif
drha0733842005-12-29 01:11:365041void sqlite3DropTable(Parse*, SrcList*, int, int);
drhfaacf172011-08-12 01:51:455042void sqlite3CodeDropTable(Parse*, Table*, int, int);
dan1feeaed2010-07-23 15:41:475043void sqlite3DeleteTable(sqlite3*, Table*);
drh82fc1b62023-12-06 18:25:415044void sqlite3DeleteTableGeneric(sqlite3*, void*);
dancf8f2892018-08-09 20:47:015045void sqlite3FreeIndex(sqlite3*, Index*);
drh0b9f50d2009-06-23 20:28:535046#ifndef SQLITE_OMIT_AUTOINCREMENT
5047 void sqlite3AutoincrementBegin(Parse *pParse);
5048 void sqlite3AutoincrementEnd(Parse *pParse);
5049#else
5050# define sqlite3AutoincrementBegin(X)
5051# define sqlite3AutoincrementEnd(X)
5052#endif
drh46d2e5c2018-04-12 13:15:435053void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
drhc1431142019-10-17 17:54:055054#ifndef SQLITE_OMIT_GENERATED_COLUMNS
drhdd6cc9b2019-10-19 18:47:275055 void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
drhc1431142019-10-17 17:54:055056#endif
drh6c535152012-02-02 03:38:305057void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
dan5496d6a2018-08-13 17:14:265058IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
danielk19774adee202004-05-08 08:23:195059int sqlite3IdListIndex(IdList*,const char*);
drh29c992c2019-01-17 15:40:415060SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
dan69887c92020-04-27 20:55:335061SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
drh29c992c2019-01-17 15:40:415062SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
drh1521ca42024-08-19 22:48:305063void sqlite3SubqueryDelete(sqlite3*,Subquery*);
5064Select *sqlite3SubqueryDetach(sqlite3*,SrcItem*);
5065int sqlite3SrcItemAttachSubquery(Parse*, SrcItem*, Select*, int);
danielk1977b1c685b2008-10-06 16:18:395066SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
drhd44f8b22022-04-07 01:11:135067 Token*, Select*, OnOrUsing*);
danielk1977b1c685b2008-10-06 16:18:395068void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
drh01d230c2015-08-19 17:11:375069void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
drh76012942021-02-21 21:04:545070int sqlite3IndexedByLookup(Parse *, SrcItem *);
drhfdc621a2022-04-16 19:13:165071void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
danielk19774adee202004-05-08 08:23:195072void sqlite3SrcListAssignCursors(Parse*, SrcList*);
drh633e6d52008-07-28 19:34:535073void sqlite3IdListDelete(sqlite3*, IdList*);
drhd44f8b22022-04-07 01:11:135074void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
drh633e6d52008-07-28 19:34:535075void sqlite3SrcListDelete(sqlite3*, SrcList*);
drhcc803b22025-02-21 20:35:375076Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**);
drh62340f82016-05-31 21:18:155077void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
5078 Expr*, int, int, u8);
drh4d91a702006-01-04 15:54:365079void sqlite3DropIndex(Parse*, SrcList*, int);
drh7d10d5a2008-08-20 16:35:105080int sqlite3Select(Parse*, Select*, SelectDest*);
drh17435752007-08-16 04:30:385081Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
drh8c0833f2017-11-14 23:48:235082 Expr*,ExprList*,u32,Expr*);
drh633e6d52008-07-28 19:34:535083void sqlite3SelectDelete(sqlite3*, Select*);
drh82fc1b62023-12-06 18:25:415084void sqlite3SelectDeleteGeneric(sqlite3*,void*);
danielk19774adee202004-05-08 08:23:195085Table *sqlite3SrcListLookup(Parse*, SrcList*);
drh3cbf38c2023-03-28 11:18:045086int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
danielk1977c00da102006-01-07 13:21:045087void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
shane273f6192008-10-10 04:34:165088#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
drh8c0833f2017-11-14 23:48:235089Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
shane4281bd42008-10-07 05:27:115090#endif
drh3b26b2b2021-12-01 19:17:145091void sqlite3CodeChangeCount(Vdbe*,int,const char*);
drh8c0833f2017-11-14 23:48:235092void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
drheac9fab2018-04-16 13:00:505093void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
5094 Upsert*);
drh895bab32022-01-27 16:14:505095WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
5096 ExprList*,Select*,u16,int);
danielk19774adee202004-05-08 08:23:195097void sqlite3WhereEnd(WhereInfo*);
drhc3489bb2016-02-25 16:04:595098LogEst sqlite3WhereOutputRowCount(WhereInfo*);
drh6f328482013-06-05 23:39:345099int sqlite3WhereIsDistinct(WhereInfo*);
5100int sqlite3WhereIsOrdered(WhereInfo*);
drh6ee5a7b2018-09-08 20:09:465101int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
drhd1930572021-01-13 15:23:175102void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
dan374cd782014-04-21 13:21:565103int sqlite3WhereIsSorted(WhereInfo*);
drh6f328482013-06-05 23:39:345104int sqlite3WhereContinueLabel(WhereInfo*);
5105int sqlite3WhereBreakLabel(WhereInfo*);
drhfc8d4f92013-11-08 15:19:465106int sqlite3WhereOkOnePass(WhereInfo*, int*);
drhb0264ee2015-09-14 14:45:505107#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
5108#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
5109#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
drhbe3da242019-12-29 00:52:415110int sqlite3WhereUsesDeferredSeek(WhereInfo*);
drh1f9ca2c2015-08-25 16:57:525111void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
drha748fdc2012-03-28 01:34:475112int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
drh6df9c4b2019-10-18 12:52:085113void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
drhb21e7c72008-06-22 12:37:575114void sqlite3ExprCodeMove(Parse*, int, int, int);
dan89b04f32024-05-24 18:31:395115void sqlite3ExprToRegister(Expr *pExpr, int iReg);
drh05a86c52014-02-16 01:55:495116void sqlite3ExprCode(Parse*, Expr*, int);
drhe70fa7f2019-10-22 21:01:345117#ifndef SQLITE_OMIT_GENERATED_COLUMNS
drh79cf2b72021-07-31 20:30:415118void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
drhe70fa7f2019-10-22 21:01:345119#endif
drh1c75c9d2015-12-21 15:22:135120void sqlite3ExprCodeCopy(Parse*, Expr*, int);
drh05a86c52014-02-16 01:55:495121void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
drh9b258c52020-03-11 19:41:495122int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
drh74cc1092025-07-18 12:10:155123void sqlite3ExprNullRegisterRange(Parse*, int, int);
drh2dcef112008-01-12 19:03:485124int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
drh678ccce2008-03-31 18:19:545125int sqlite3ExprCodeTarget(Parse*, Expr*, int);
drh5579d592015-08-26 14:01:415126int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
drhd1a01ed2013-11-21 16:08:525127#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
5128#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
drh5579d592015-08-26 14:01:415129#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
dan257c13f2016-11-10 20:14:065130#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
danielk19774adee202004-05-08 08:23:195131void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
5132void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
drh72bc8202015-06-11 13:58:355133void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
drh9bb575f2004-09-06 17:24:115134Table *sqlite3FindTable(sqlite3*,const char*, const char*);
drh4d249e62016-06-10 22:49:015135#define LOCATE_VIEW 0x01
5136#define LOCATE_NOERR 0x02
5137Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
drha4a871c2021-11-04 14:04:205138const char *sqlite3PreferredTableName(const char*);
drh76012942021-02-21 21:04:545139Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
drh9bb575f2004-09-06 17:24:115140Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
5141void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
5142void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
drh2f6239e2018-12-08 00:43:085143void sqlite3Vacuum(Parse*,Token*,Expr*);
5144int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
drhb6dad522021-09-24 16:14:475145char *sqlite3NameFromToken(sqlite3*, const Token*);
drh1580d502021-09-25 17:07:575146int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
5147int sqlite3ExprCompareSkip(Expr*,Expr*,int);
5148int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
5149int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
drh038158e2023-06-02 18:05:545150int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
drh89636622020-06-07 17:33:185151void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
drhd2b3e232008-01-23 14:51:495152void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
5153void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
drh2409f8a2016-07-27 18:27:025154int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
drh90cf38b2021-11-08 23:24:005155int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
danielk19774adee202004-05-08 08:23:195156Vdbe *sqlite3GetVdbe(Parse*);
drhd12602a2016-12-07 15:49:025157#ifndef SQLITE_UNTESTABLE
drh2fa18682008-03-19 14:15:345158void sqlite3PrngSaveState(void);
5159void sqlite3PrngRestoreState(void);
drhf5ed7ad2015-06-15 14:43:255160#endif
drh0f198a72012-02-13 16:43:165161void sqlite3RollbackAll(sqlite3*,int);
danielk19774adee202004-05-08 08:23:195162void sqlite3CodeVerifySchema(Parse*, int);
dan57966752011-04-09 17:32:585163void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
drh684917c2004-10-05 02:41:425164void sqlite3BeginTransaction(Parse*, int);
drh07a3b112017-07-06 01:28:025165void sqlite3EndTransaction(Parse*,int);
danielk1977fd7f0452008-12-17 17:30:265166void sqlite3Savepoint(Parse*, int, Token*);
5167void sqlite3CloseSavepoints(sqlite3 *);
drh4245c402012-06-02 14:32:215168void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
drh0cbec592020-01-03 02:20:375169u32 sqlite3IsTrueOrFalse(const char*);
drh171d16b2018-02-26 20:15:545170int sqlite3ExprIdToTrueFalse(Expr*);
drh96acafb2018-02-27 14:49:255171int sqlite3ExprTruthValue(const Expr*);
drhf6965912024-03-16 13:18:485172int sqlite3ExprIsConstant(Parse*,Expr*);
drhfeada2d2014-09-24 13:20:225173int sqlite3ExprIsConstantOrFunction(Expr*, u8);
danab31a842017-04-29 20:53:095174int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
drh6951c492024-04-06 18:30:095175int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int);
drhffc648c2015-08-13 21:38:095176#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh5b88bc42013-12-07 23:35:215177int sqlite3ExprContainsSubquery(Expr*);
drhffc648c2015-08-13 21:38:095178#endif
drh4703b7d2024-06-06 15:03:165179int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
drh039fc322009-11-17 18:31:475180int sqlite3ExprCanBeNull(const Expr*);
5181int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
danielk19774adee202004-05-08 08:23:195182int sqlite3IsRowid(const char*);
dan81b70d92023-09-15 18:36:515183const char *sqlite3RowidAlias(Table *pTab);
danf0ee1d32015-09-12 19:26:115184void sqlite3GenerateRowDelete(
5185 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
5186void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
drh1c2c0b72014-01-04 19:27:055187int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
drh87744512014-04-13 19:15:495188void sqlite3ResolvePartIdxLabel(Parse*,int);
drhe9816d82018-09-15 21:38:485189int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
drhf8ffb272013-11-01 17:08:565190void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
drh788d55a2018-04-13 01:15:095191 u8,u8,int,int*,int*,Upsert*);
drhd447dce2017-01-25 20:55:115192#ifdef SQLITE_ENABLE_NULL_TRIM
5193 void sqlite3SetMakeRecordP5(Vdbe*,Table*);
5194#else
5195# define sqlite3SetMakeRecordP5(A,B)
5196#endif
drh26198bb2013-10-31 11:15:095197void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
danfd261ec2015-10-22 20:54:335198int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
danielk19774adee202004-05-08 08:23:195199void sqlite3BeginWriteOperation(Parse*, int, int);
drhff738bc2009-09-24 00:09:585200void sqlite3MultiWrite(Parse*);
5201void sqlite3MayAbort(Parse*);
drhf9c8ce32013-11-05 13:33:555202void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
5203void sqlite3UniqueConstraint(Parse*, int, Index*);
5204void sqlite3RowidConstraint(Parse*, int, Table*);
drhb6dad522021-09-24 16:14:475205Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
5206ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
5207SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
5208IdList *sqlite3IdListDup(sqlite3*,const IdList*);
5209Select *sqlite3SelectDup(sqlite3*,const Select*,int);
drh19efd0d2018-12-05 17:48:575210FuncDef *sqlite3FunctionSearch(int,const char*);
drh80738d92016-02-15 00:34:165211void sqlite3InsertBuiltinFuncs(FuncDef*,int);
5212FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
drhb6205d42025-02-24 13:51:245213void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int);
drha357a902025-02-25 11:47:345214int sqlite3AppendOneUtf8Character(char*, u32);
drh80738d92016-02-15 00:34:165215void sqlite3RegisterBuiltinFunctions(void);
drh777c5382008-08-21 20:21:345216void sqlite3RegisterDateTimeFunctions(void);
drh9dbf96b2022-01-06 01:40:095217void sqlite3RegisterJsonFunctions(void);
drh80738d92016-02-15 00:34:165218void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
drh9dbf96b2022-01-06 01:40:095219#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
5220 int sqlite3JsonTableFunctions(sqlite3*);
5221#endif
drh7e8b8482008-01-23 03:03:055222int sqlite3SafetyCheckOk(sqlite3*);
5223int sqlite3SafetyCheckSickOrOk(sqlite3*);
drh9cbf3422008-01-17 16:22:135224void sqlite3ChangeCookie(Parse*, int);
dan26d61e52021-06-11 11:14:245225With *sqlite3WithDup(sqlite3 *db, With *p);
shanefa4e62f2008-09-01 21:59:425226
5227#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
drh8c0833f2017-11-14 23:48:235228void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
shanefa4e62f2008-09-01 21:59:425229#endif
drhb7f91642004-10-31 02:22:475230
5231#ifndef SQLITE_OMIT_TRIGGER
5232 void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
drh60218d22007-04-06 11:26:005233 Expr*,int, int);
drhb7f91642004-10-31 02:22:475234 void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
drhfdd48a72006-09-11 23:45:485235 void sqlite3DropTrigger(Parse*, SrcList*, int);
drh74161702006-02-24 02:53:495236 void sqlite3DropTriggerPtr(Parse*, Trigger*);
danielk19772f886d12009-02-28 10:47:415237 Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
5238 Trigger *sqlite3TriggerList(Parse *, Table *);
dan165921a2009-08-28 18:53:455239 void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
dan94d7f502009-09-24 09:05:495240 int, int, int);
dan1da40a32009-09-19 17:00:315241 void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
drhb7f91642004-10-31 02:22:475242 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
drh633e6d52008-07-28 19:34:535243 void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
drhf259df52017-12-27 20:38:355244 TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
5245 const char*,const char*);
dan5be60c52018-08-15 20:28:395246 TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
drh46d2e5c2018-04-12 13:15:435247 Select*,u8,Upsert*,
drh2c2e8442018-04-07 15:04:055248 const char*,const char*);
dane7877b22020-07-14 19:51:015249 TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
5250 Expr*, u8, const char*,const char*);
dan5be60c52018-08-15 20:28:395251 TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
drhf259df52017-12-27 20:38:355252 const char*,const char*);
drh633e6d52008-07-28 19:34:535253 void sqlite3DeleteTrigger(sqlite3*, Trigger*);
drhb7f91642004-10-31 02:22:475254 void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
danbb5f1682009-11-27 12:12:345255 u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
dane7877b22020-07-14 19:51:015256 SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
dan65a7cd12009-09-01 12:16:015257# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
drhc149f182015-09-29 13:25:155258# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
drhb7f91642004-10-31 02:22:475259#else
danielk197762c14b32008-11-19 09:05:265260# define sqlite3TriggersExist(B,C,D,E,F) 0
drh633e6d52008-07-28 19:34:535261# define sqlite3DeleteTrigger(A,B)
drhcdd536f2006-03-17 00:04:035262# define sqlite3DropTriggerPtr(A,B)
drhb7f91642004-10-31 02:22:475263# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
dan94d7f502009-09-24 09:05:495264# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
dan75cbd982009-09-21 16:06:035265# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
danielk19772943c372009-04-07 14:14:225266# define sqlite3TriggerList(X, Y) 0
dan65a7cd12009-09-01 12:16:015267# define sqlite3ParseToplevel(p) p
drhc149f182015-09-29 13:25:155268# define sqlite3IsToplevel(p) 1
danbb5f1682009-11-27 12:12:345269# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
dane7877b22020-07-14 19:51:015270# define sqlite3TriggerStepSrc(A,B) 0
drhb7f91642004-10-31 02:22:475271#endif
5272
danielk19774adee202004-05-08 08:23:195273int sqlite3JoinType(Parse*, Token*, Token*, Token*);
dan6e6d9832021-02-16 20:43:365274int sqlite3ColumnIndex(Table *pTab, const char *zCol);
drh815b7822022-04-20 15:07:395275void sqlite3SrcItemColumnUsed(SrcItem*,int);
drh3a6e4c52022-04-11 12:38:065276void sqlite3SetJoinExpr(Expr*,int,u32);
danielk19770202b292004-06-09 09:55:165277void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
danielk19774adee202004-05-08 08:23:195278void sqlite3DeferForeignKey(Parse*, int);
drhed6c8672003-01-12 18:02:165279#ifndef SQLITE_OMIT_AUTHORIZATION
drh728b5772007-09-18 15:55:075280 void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
danielk19774adee202004-05-08 08:23:195281 int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
5282 void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
5283 void sqlite3AuthContextPop(AuthContext*);
dan02470b22009-10-03 07:04:115284 int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
drhed6c8672003-01-12 18:02:165285#else
danielk1977b5258c32007-10-04 18:11:155286# define sqlite3AuthRead(a,b,c,d)
danielk19774adee202004-05-08 08:23:195287# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
5288# define sqlite3AuthContextPush(a,b,c)
5289# define sqlite3AuthContextPop(a) ((void)(a))
drhed6c8672003-01-12 18:02:165290#endif
dan465c2b82020-03-21 15:10:405291int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
danielk1977f744bb52005-12-06 17:19:115292void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
5293void sqlite3Detach(Parse*, Expr*);
drhd100f692013-10-03 15:39:445294void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
danielk19774adee202004-05-08 08:23:195295int sqlite3FixSrcList(DbFixer*, SrcList*);
5296int sqlite3FixSelect(DbFixer*, Select*);
5297int sqlite3FixExpr(DbFixer*, Expr*);
danielk19774adee202004-05-08 08:23:195298int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
drh12b198f2023-06-26 19:35:205299
drh8a3884e2019-05-29 21:18:275300int sqlite3RealSameAsInt(double,sqlite3_int64);
drh26e817f2022-08-08 16:25:135301i64 sqlite3RealToI64(double);
drhfbde3f52023-01-03 18:51:185302int sqlite3Int64ToText(i64,char*);
drh9339da12010-09-30 00:50:495303int sqlite3AtoF(const char *z, double*, int, u8);
drhfec19aa2004-05-19 20:41:035304int sqlite3GetInt32(const char *, int*);
drhabc38152020-07-22 13:38:045305int sqlite3GetUInt32(const char*, u32*);
drh60ac3f42010-11-23 18:59:275306int sqlite3Atoi(const char*);
drhf0f44b72017-07-12 12:19:335307#ifndef SQLITE_OMIT_UTF16
drhf8305e42024-09-19 13:39:065308int sqlite3Utf16ByteLen(const void *pData, int nByte, int nChar);
drhf0f44b72017-07-12 12:19:335309#endif
drhee858132007-05-08 20:37:385310int sqlite3Utf8CharLen(const char *pData, int nByte);
drh42610962012-09-17 18:56:325311u32 sqlite3Utf8Read(const u8**);
drh001d1e72023-12-13 14:31:155312int sqlite3Utf8ReadLimited(const u8*, int, u32*);
drhbf539c42013-10-05 18:16:025313LogEst sqlite3LogEst(u64);
5314LogEst sqlite3LogEstAdd(LogEst,LogEst);
drhbf539c42013-10-05 18:16:025315LogEst sqlite3LogEstFromDouble(double);
drhbf539c42013-10-05 18:16:025316u64 sqlite3LogEstToInt(LogEst);
drh9bf755c2016-12-23 03:59:315317VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
5318const char *sqlite3VListNumToName(VList*,int);
5319int sqlite3VListNameToNum(VList*,const char*,int);
shane3f8d5cf2008-04-24 19:15:095320
5321/*
5322** Routines to read and write variable-length integers. These used to
5323** be defined locally, but now we use the varint routines in the util.c
drh2f2b2b82014-08-22 18:48:255324** file.
shane3f8d5cf2008-04-24 19:15:095325*/
drh35b5a332008-04-05 18:41:425326int sqlite3PutVarint(unsigned char*, u64);
drh1bd10f82008-12-10 21:19:565327u8 sqlite3GetVarint(const unsigned char *, u64 *);
5328u8 sqlite3GetVarint32(const unsigned char *, u32 *);
danielk1977192ac1d2004-05-10 07:17:305329int sqlite3VarintLen(u64 v);
shane3f8d5cf2008-04-24 19:15:095330
5331/*
drh2f2b2b82014-08-22 18:48:255332** The common case is for a varint to be a single byte. They following
5333** macros handle the common case without a procedure call, but then call
5334** the procedure for larger varints.
shane3f8d5cf2008-04-24 19:15:095335*/
drh4bde3702013-02-19 18:34:125336#define getVarint32(A,B) \
5337 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
drh02a95eb2020-01-28 20:27:425338#define getVarint32NR(A,B) \
5339 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
drh4bde3702013-02-19 18:34:125340#define putVarint32(A,B) \
5341 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
drh2f2b2b82014-08-22 18:48:255342 sqlite3PutVarint((A),(B)))
shane3f8d5cf2008-04-24 19:15:095343#define getVarint sqlite3GetVarint
5344#define putVarint sqlite3PutVarint
5345
5346
drhe9107692015-08-25 19:20:045347const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
drh5fdb9a32022-11-01 00:52:225348char *sqlite3TableAffinityStr(sqlite3*,const Table*);
drh57bf4a82014-02-17 14:59:225349void sqlite3TableAffinity(Vdbe*, Table*, int);
drhe7375bf2020-03-10 19:24:385350char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
5351int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
drhb6dad522021-09-24 16:14:475352char sqlite3TableColumnAffinity(const Table*,int);
drhe7375bf2020-03-10 19:24:385353char sqlite3ExprAffinity(const Expr *pExpr);
drhed07d0e2022-12-14 14:41:355354int sqlite3ExprDataType(const Expr *pExpr);
drh9339da12010-09-30 00:50:495355int sqlite3Atoi64(const char*, i64*, int, u8);
drh9296c182014-07-23 13:40:495356int sqlite3DecOrHexToI64(const char*, i64*);
drh13f40da2014-08-22 18:00:115357void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
5358void sqlite3Error(sqlite3*,int);
drh88efc792021-01-01 18:23:565359void sqlite3ErrorClear(sqlite3*);
drh1b9f2142016-03-17 16:01:235360void sqlite3SystemError(sqlite3*,int);
stephan8292aa72024-05-10 09:26:535361#if !defined(SQLITE_OMIT_BLOB_LITERAL)
drhca48c902008-01-18 14:08:245362void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
stephan8292aa72024-05-10 09:26:535363#endif
dancd74b612011-04-22 19:37:325364u8 sqlite3HexToInt(int h);
danielk1977ef2cb632004-05-29 02:37:195365int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
mistachkindd08ca02013-04-30 07:58:155366
mistachkin5824d442015-04-28 23:34:105367#if defined(SQLITE_NEED_ERR_NAME)
mistachkinf2c1c992013-04-28 01:44:435368const char *sqlite3ErrName(int);
mistachkindd08ca02013-04-30 07:58:155369#endif
5370
drh8d889af2021-05-08 17:18:235371#ifndef SQLITE_OMIT_DESERIALIZE
drhac442f42018-01-03 01:28:465372int sqlite3MemdbInit(void);
drhcf3107c2022-11-19 00:08:355373int sqlite3IsMemdb(const sqlite3_vfs*);
5374#else
5375# define sqlite3IsMemdb(X) 0
drhac442f42018-01-03 01:28:465376#endif
5377
danielk1977f20b21c2004-05-31 23:56:425378const char *sqlite3ErrStr(int);
danielk19778a414492004-06-29 08:59:355379int sqlite3ReadSchema(Parse *pParse);
drhc4a64fa2009-05-11 20:53:285380CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
drhefad2e22018-07-27 16:57:115381int sqlite3IsBinary(const CollSeq*);
drhc4a64fa2009-05-11 20:53:285382CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
drh42a630b2020-03-05 16:13:245383void sqlite3SetTextEncoding(sqlite3 *db, u8);
drhe7375bf2020-03-10 19:24:385384CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
5385CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
5386int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
drhb6dad522021-09-24 16:14:475387Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
5388Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
drh0a8a4062012-12-07 18:38:165389Expr *sqlite3ExprSkipCollate(Expr*);
drh0d950af2019-08-22 16:38:425390Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
danielk19777cedc8d2004-06-10 10:50:085391int sqlite3CheckCollSeq(Parse *, CollSeq *);
drh0f1c2eb2018-11-03 17:31:485392int sqlite3WritableSchema(sqlite3*);
drhc5a93d42019-08-12 00:08:075393int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
dan2c718872021-06-22 18:32:055394void sqlite3VdbeSetChanges(sqlite3 *, i64);
drh158b9cb2011-03-05 20:59:465395int sqlite3AddInt64(i64*,i64);
5396int sqlite3SubInt64(i64*,i64);
5397int sqlite3MulInt64(i64*,i64);
drhd50ffc42011-03-08 02:38:285398int sqlite3AbsInt32(int);
drh81cc5162011-05-17 20:36:215399#ifdef SQLITE_ENABLE_8_3_NAMES
5400void sqlite3FileSuffix3(const char*, char*);
5401#else
5402# define sqlite3FileSuffix3(X,Y)
5403#endif
drheac5bd72014-07-25 21:35:395404u8 sqlite3GetBoolean(const char *z,u8);
danielk19774e6af132004-06-10 14:01:085405
drhb21c8cd2007-08-21 19:33:565406const void *sqlite3ValueText(sqlite3_value*, u8);
drh6bc4baf2023-07-27 20:28:295407int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
drhb21c8cd2007-08-21 19:33:565408int sqlite3ValueBytes(sqlite3_value*, u8);
mistachkinbfc9b3f2016-02-15 22:01:245409void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
danielk19771e536952007-08-16 10:09:015410 void(*)(void*));
drha3cc0072013-12-13 16:23:555411void sqlite3ValueSetNull(sqlite3_value*);
danielk19774e6af132004-06-10 14:01:085412void sqlite3ValueFree(sqlite3_value*);
drh0c8f4032019-05-03 21:17:285413#ifndef SQLITE_UNTESTABLE
5414void sqlite3ResultIntReal(sqlite3_context*);
5415#endif
danielk19771e536952007-08-16 10:09:015416sqlite3_value *sqlite3ValueNew(sqlite3 *);
drhf0f44b72017-07-12 12:19:335417#ifndef SQLITE_OMIT_UTF16
danb7dca7d2010-03-05 16:32:125418char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
drhf0f44b72017-07-12 12:19:335419#endif
drh1580d502021-09-25 17:07:575420int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
drhb21c8cd2007-08-21 19:33:565421void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
drh46c99e02007-08-27 23:26:595422#ifndef SQLITE_AMALGAMATION
drha6c2ed92009-11-14 23:22:235423extern const unsigned char sqlite3OpcodeProperty[];
drhf19aa5f2015-12-30 16:51:205424extern const char sqlite3StrBINARY[];
drhc2df4d62021-07-30 23:30:305425extern const unsigned char sqlite3StdTypeLen[];
5426extern const char sqlite3StdTypeAffinity[];
5427extern const char *sqlite3StdType[];
drh4e5ffc52004-08-31 00:52:375428extern const unsigned char sqlite3UpperToLower[];
drh1af3fd52021-03-28 23:37:565429extern const unsigned char *sqlite3aLTb;
5430extern const unsigned char *sqlite3aEQb;
5431extern const unsigned char *sqlite3aGTb;
danielk197778ca0e72009-01-20 16:53:395432extern const unsigned char sqlite3CtypeMap[];
danielk1977075c23a2008-09-01 18:34:205433extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
drh80738d92016-02-15 00:34:165434extern FuncDefHash sqlite3BuiltinFunctions;
drhf83dc1e2010-06-03 12:09:525435#ifndef SQLITE_OMIT_WSD
drhddb68e12009-02-05 16:53:435436extern int sqlite3PendingByte;
drh46c99e02007-08-27 23:26:595437#endif
drh9216de82020-06-11 00:57:095438#endif /* SQLITE_AMALGAMATION */
drh35043cc2018-02-12 20:27:345439#ifdef VDBE_PROFILE
5440extern sqlite3_uint64 sqlite3NProfileCnt;
5441#endif
drhabc38152020-07-22 13:38:045442void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
drh4343fea2004-11-05 23:46:155443void sqlite3Reindex(Parse*, Token*, Token*);
drh545f5872010-04-24 14:02:595444void sqlite3AlterFunctions(void);
danielk19779fd2a9a2004-11-12 13:42:305445void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
dancf8f2892018-08-09 20:47:015446void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
danielk19779fd2a9a2004-11-12 13:42:305447int sqlite3GetToken(const unsigned char *, int *);
drh2958a4e2004-11-12 03:56:155448void sqlite3NestedParse(Parse*, const char*, ...);
drhba968db2018-07-24 22:02:125449void sqlite3ExpirePreparedStatements(sqlite3*, int);
drh50ef6712019-02-22 23:29:565450void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
drh85bcdce2018-12-23 21:27:295451int sqlite3CodeSubselect(Parse*, Expr*);
drh7d10d5a2008-08-20 16:35:105452void sqlite3SelectPrep(Parse*, Select*, NameContext*);
drh76012942021-02-21 21:04:545453int sqlite3ExpandSubquery(Parse*, SrcItem*);
dan923cadb2015-06-23 12:19:555454void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
drhc4938ea2019-12-13 00:49:425455int sqlite3MatchEName(
5456 const struct ExprList_item*,
5457 const char*,
5458 const char*,
dan81b70d92023-09-15 18:36:515459 const char*,
dan63702bc2023-09-15 20:57:055460 int*
drhc4938ea2019-12-13 00:49:425461);
drh74a07982020-03-21 23:10:385462Bitmask sqlite3ExprColUsed(Expr*);
drhd44390c2020-04-06 18:16:315463u8 sqlite3StrIHash(const char*);
drh7d10d5a2008-08-20 16:35:105464int sqlite3ResolveExprNames(NameContext*, Expr*);
drh01d230c2015-08-19 17:11:375465int sqlite3ResolveExprListNames(NameContext*, ExprList*);
drh7d10d5a2008-08-20 16:35:105466void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
drhee751fa2019-01-02 14:34:465467int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
drh7d10d5a2008-08-20 16:35:105468int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
danielk1977c7538b52009-07-27 10:05:045469void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
danielk197719a8e7e2005-03-17 05:03:385470void sqlite3AlterFinishAddColumn(Parse *, Token *);
5471void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
drhb6dad522021-09-24 16:14:475472void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
5473const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
5474void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
dan8900a482018-09-05 14:36:055475void sqlite3RenameExprUnmap(Parse*, Expr*);
dane8ab40d2018-09-12 08:51:485476void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
drh79e72a52012-10-05 14:43:405477CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
dan2e3a5a82018-04-16 21:12:425478char sqlite3AffinityType(const char*, Column*);
drh9f18e8a2005-07-08 12:13:045479void sqlite3Analyze(Parse*, Token*, Token*);
drh783e1592020-05-06 20:55:385480int sqlite3InvokeBusyHandler(BusyHandler*);
drhff2d5ea2005-07-23 00:41:485481int sqlite3FindDb(sqlite3*, Token*);
danielk197704103022009-02-03 16:51:245482int sqlite3FindDbName(sqlite3 *, const char *);
drhcf1be452007-05-12 12:08:515483int sqlite3AnalysisLoad(sqlite3*,int iDB);
dand46def72010-07-24 11:28:285484void sqlite3DeleteIndexSamples(sqlite3*,Index*);
drh51147ba2005-07-23 22:59:555485void sqlite3DefaultRowEst(Index*);
drh55ef4d92005-08-14 01:20:375486void sqlite3RegisterLikeFunctions(sqlite3*, int);
drhd64fe2f2005-08-28 17:00:235487int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
drhb6ee6602011-04-04 13:40:535488void sqlite3SchemaClear(void *);
danielk19771e536952007-08-16 10:09:015489Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
danielk1977e501b892006-01-09 06:29:475490int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
drhad124322013-10-23 13:30:585491KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
drh2ec2fb22013-11-06 19:59:235492void sqlite3KeyInfoUnref(KeyInfo*);
5493KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
5494KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
danf9eae182018-05-21 19:45:115495KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
drhaae0f742021-03-04 16:03:325496const char *sqlite3SelectOpName(int);
dan9105fd52019-08-19 17:26:325497int sqlite3HasExplicitNulls(Parse*, ExprList*);
danf9eae182018-05-21 19:45:115498
drh2ec2fb22013-11-06 19:59:235499#ifdef SQLITE_DEBUG
5500int sqlite3KeyInfoIsWriteable(KeyInfo*);
5501#endif
mistachkinbfc9b3f2016-02-15 22:01:245502int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
danielk1977771151b2006-01-17 13:21:405503 void (*)(sqlite3_context*,int,sqlite3_value **),
larrybrbc917382023-06-07 08:40:315504 void (*)(sqlite3_context*,int,sqlite3_value **),
dan660af932018-06-18 16:55:225505 void (*)(sqlite3_context*),
5506 void (*)(sqlite3_context*),
larrybrbc917382023-06-07 08:40:315507 void (*)(sqlite3_context*,int,sqlite3_value **),
dand2199f02010-08-27 17:48:525508 FuncDestructor *pDestructor
5509);
drh92011842018-05-26 16:00:265510void sqlite3NoopDestructor(void*);
drh3cdb1392022-01-24 12:48:545511void *sqlite3OomFault(sqlite3*);
drh4a642b62016-02-05 01:55:275512void sqlite3OomClear(sqlite3*);
danielk197754f01982006-01-18 15:25:175513int sqlite3ApiExit(sqlite3 *db, int);
danielk1977ddfb2f02006-02-17 12:25:145514int sqlite3OpenTempDatabase(Parse *);
drh69dab1d2006-06-27 14:37:205515
drhf02cc9a2023-07-25 15:08:185516char *sqlite3RCStrRef(char*);
drh43dc31c2023-10-17 19:33:525517void sqlite3RCStrUnref(void*);
drhf02cc9a2023-07-25 15:08:185518char *sqlite3RCStrNew(u64);
drhf02cc9a2023-07-25 15:08:185519char *sqlite3RCStrResize(char*,u64);
drhf02cc9a2023-07-25 15:08:185520
drhc0490572015-05-02 11:45:535521void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
drh79b9bc42022-12-21 19:11:565522int sqlite3StrAccumEnlarge(StrAccum*, i64);
drhade86482007-11-28 22:36:405523char *sqlite3StrAccumFinish(StrAccum*);
drhf06db3e2021-10-01 00:25:065524void sqlite3StrAccumSetError(StrAccum*, u8);
drh5bf47152021-10-03 00:12:435525void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
drh1013c932008-01-06 00:25:215526void sqlite3SelectDestInit(SelectDest*,int,int);
danf7b0b0a2009-10-19 15:52:325527Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
drhf62641e2021-12-24 20:22:135528void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
drh4f77c922022-02-05 23:11:195529void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*);
danielk19771e536952007-08-16 10:09:015530
danielk197704103022009-02-03 16:51:245531void sqlite3BackupRestart(sqlite3_backup *);
5532void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
5533
danf9b2e052016-08-02 17:45:005534#ifndef SQLITE_OMIT_SUBQUERY
5535int sqlite3ExprCheckIN(Parse*, Expr*);
5536#else
5537# define sqlite3ExprCheckIN(x,y) SQLITE_OK
5538#endif
5539
drh175b8f02019-08-08 15:24:175540#ifdef SQLITE_ENABLE_STAT4
dand66e5792016-08-03 16:14:335541int sqlite3Stat4ProbeSetValue(
5542 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
danb0b82902014-06-26 20:21:465543int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
dan7a419232013-08-06 20:01:435544void sqlite3Stat4ProbeFree(UnpackedRecord*);
danb0b82902014-06-26 20:21:465545int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
dand66e5792016-08-03 16:14:335546char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
drh9fecc542013-08-27 20:16:485547#endif
dan7a419232013-08-06 20:01:435548
drh95bdbbb2007-07-23 19:31:165549/*
5550** The interface to the LEMON-generated parser
5551*/
drhd26cc542017-01-28 20:46:375552#ifndef SQLITE_AMALGAMATION
drhfb32c442018-04-21 13:51:425553 void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
drhd26cc542017-01-28 20:46:375554 void sqlite3ParserFree(void*, void(*)(void*));
5555#endif
drhfb32c442018-04-21 13:51:425556void sqlite3Parser(void*, int, Token);
dan59ff4252018-06-29 17:44:525557int sqlite3ParserFallback(int);
drhec424a52008-07-25 15:39:035558#ifdef YYTRACKMAXSTACKDEPTH
5559 int sqlite3ParserStackPeak(void*);
5560#endif
drh95bdbbb2007-07-23 19:31:165561
drh7aaa8782009-05-20 02:40:455562void sqlite3AutoLoadExtensions(sqlite3*);
drh69dab1d2006-06-27 14:37:205563#ifndef SQLITE_OMIT_LOAD_EXTENSION
5564 void sqlite3CloseExtensions(sqlite3*);
5565#else
5566# define sqlite3CloseExtensions(X)
5567#endif
danielk1977e3026632004-06-22 11:29:025568
danielk1977c00da102006-01-07 13:21:045569#ifndef SQLITE_OMIT_SHARED_CACHE
drhabc38152020-07-22 13:38:045570 void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
danielk1977c00da102006-01-07 13:21:045571#else
5572 #define sqlite3TableLock(v,w,x,y,z)
5573#endif
5574
drh53c14022007-05-10 17:23:115575#ifdef SQLITE_TEST
5576 int sqlite3Utf8To8(unsigned char*);
5577#endif
5578
drhb9bb7c12006-06-11 23:41:555579#ifdef SQLITE_OMIT_VIRTUALTABLE
larrybr998e9102021-09-15 14:48:025580# define sqlite3VtabClear(D,T)
danielk19779dbee7d2008-08-02 15:32:395581# define sqlite3VtabSync(X,Y) SQLITE_OK
danielk1977f9e7dda2006-06-16 16:08:535582# define sqlite3VtabRollback(X)
5583# define sqlite3VtabCommit(X)
danielk1977093e0f62008-11-13 18:00:145584# define sqlite3VtabInSync(db) 0
mistachkinbfc9b3f2016-02-15 22:01:245585# define sqlite3VtabLock(X)
danielk1977595a5232009-07-24 17:58:535586# define sqlite3VtabUnlock(X)
drhcc5979d2019-08-16 22:58:295587# define sqlite3VtabModuleUnref(D,X)
danielk1977595a5232009-07-24 17:58:535588# define sqlite3VtabUnlockList(X)
dana311b802011-04-26 19:21:345589# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
drhc3c8dac2011-10-12 19:04:075590# define sqlite3GetVTable(X,Y) ((VTable*)0)
drhb9bb7c12006-06-11 23:41:555591#else
dan1feeaed2010-07-23 15:41:475592 void sqlite3VtabClear(sqlite3 *db, Table*);
danbba02a92012-05-15 17:15:345593 void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
dan016f7812013-08-21 17:35:485594 int sqlite3VtabSync(sqlite3 *db, Vdbe*);
danielk1977f9e7dda2006-06-16 16:08:535595 int sqlite3VtabRollback(sqlite3 *db);
5596 int sqlite3VtabCommit(sqlite3 *db);
danielk1977595a5232009-07-24 17:58:535597 void sqlite3VtabLock(VTable *);
5598 void sqlite3VtabUnlock(VTable *);
drhcc5979d2019-08-16 22:58:295599 void sqlite3VtabModuleUnref(sqlite3*,Module*);
danielk1977595a5232009-07-24 17:58:535600 void sqlite3VtabUnlockList(sqlite3*);
dana311b802011-04-26 19:21:345601 int sqlite3VtabSavepoint(sqlite3 *, int, int);
dan016f7812013-08-21 17:35:485602 void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
drhc3c8dac2011-10-12 19:04:075603 VTable *sqlite3GetVTable(sqlite3*, Table*);
drh2fcc1592016-12-15 20:59:035604 Module *sqlite3VtabCreateModule(
5605 sqlite3*,
5606 const char*,
5607 const sqlite3_module*,
5608 void*,
5609 void(*)(void*)
5610 );
danielk1977093e0f62008-11-13 18:00:145611# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
drhb9bb7c12006-06-11 23:41:555612#endif
drh070ae3b2019-11-16 13:51:315613int sqlite3ReadOnlyShadowTables(sqlite3 *db);
drh527cbd42019-11-16 14:15:195614#ifndef SQLITE_OMIT_VIRTUALTABLE
5615 int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
drh3d863b52020-05-14 21:16:525616 int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
drhddfec002021-11-04 00:51:535617 void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
drh527cbd42019-11-16 14:15:195618#else
5619# define sqlite3ShadowTableName(A,B) 0
drh3d863b52020-05-14 21:16:525620# define sqlite3IsShadowTableOf(A,B,C) 0
drhddfec002021-11-04 00:51:535621# define sqlite3MarkAllShadowTablesOf(A,B)
drh527cbd42019-11-16 14:15:195622#endif
drh51be3872015-08-19 02:32:255623int sqlite3VtabEponymousTableInit(Parse*,Module*);
5624void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
drh4f3dd152008-04-28 18:46:435625void sqlite3VtabMakeWritable(Parse*,Table*);
drhb421b892012-01-28 19:41:535626void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
drhb9bb7c12006-06-11 23:41:555627void sqlite3VtabFinishParse(Parse*, Token*);
5628void sqlite3VtabArgInit(Parse*);
5629void sqlite3VtabArgExtend(Parse*, Token*);
danielk197778efaba2006-06-12 06:09:175630int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
danielk19777e6ebfb2006-06-12 11:24:375631int sqlite3VtabCallConnect(Parse*, Table*);
danielk19779e39ce82006-06-12 16:01:215632int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
danielk1977595a5232009-07-24 17:58:535633int sqlite3VtabBegin(sqlite3 *, VTable *);
drh46dc6312022-03-09 14:22:285634
danielk19771e536952007-08-16 10:09:015635FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
drh0669d6e2023-04-03 15:01:375636void sqlite3VtabUsesAllSchemas(Parse*);
drh601e4d42023-02-08 20:29:485637sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
drh5f18a222009-11-26 14:01:535638int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
shane4a27a282008-09-04 04:32:495639int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
drhc692df22022-01-24 15:34:555640void sqlite3ParseObjectInit(Parse*,sqlite3*);
5641void sqlite3ParseObjectReset(Parse*);
drha79e2a22021-02-21 23:44:145642void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
mistachkin8bee11a2018-10-29 17:53:235643#ifdef SQLITE_ENABLE_NORMALIZE
drh1a6c2b12018-12-10 20:01:405644char *sqlite3Normalize(Vdbe*, const char*);
mistachkin8bee11a2018-10-29 17:53:235645#endif
drhb900aaf2006-11-09 00:24:535646int sqlite3Reprepare(Vdbe*);
drhb1a6c3c2008-03-20 16:30:175647void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
drhe7375bf2020-03-10 19:24:385648CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
5649CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
drh1c514142009-04-30 12:25:105650int sqlite3TempInMemory(const sqlite3*);
dan28e53862010-04-21 06:19:125651const char *sqlite3JournalModename(int);
dan06a2d822012-10-10 09:46:295652#ifndef SQLITE_OMIT_WAL
5653 int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
5654 int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
5655#endif
drh8b471862014-01-11 13:22:175656#ifndef SQLITE_OMIT_CTE
drh745912e2021-02-22 03:04:255657 Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
drhf824b412021-02-20 14:57:165658 void sqlite3CteDelete(sqlite3*,Cte*);
5659 With *sqlite3WithAdd(Parse*,With*,Cte*);
dan7d562db2014-01-11 19:19:365660 void sqlite3WithDelete(sqlite3*,With*);
drh82fc1b62023-12-06 18:25:415661 void sqlite3WithDeleteGeneric(sqlite3*,void*);
drh24ce9442021-06-12 17:45:325662 With *sqlite3WithPush(Parse*, With*, u8);
dan4e9119d2014-01-13 15:12:235663#else
drhf824b412021-02-20 14:57:165664# define sqlite3CteNew(P,T,E,S) ((void*)0)
5665# define sqlite3CteDelete(D,C)
5666# define sqlite3CteWithAdd(P,W,C) ((void*)0)
5667# define sqlite3WithDelete(x,y)
drh9f9bdf92021-11-22 13:35:405668# define sqlite3WithPush(x,y,z) ((void*)0)
drh8b471862014-01-11 13:22:175669#endif
drh46d2e5c2018-04-12 13:15:435670#ifndef SQLITE_OMIT_UPSERT
drh2549e4c2020-12-08 14:29:035671 Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
drh46d2e5c2018-04-12 13:15:435672 void sqlite3UpsertDelete(sqlite3*,Upsert*);
5673 Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
drh926fb602024-03-08 14:01:485674 int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*);
dan2cc00422018-04-17 18:16:105675 void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
drh61e280a2020-12-11 01:17:065676 Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
5677 int sqlite3UpsertNextIsIPK(Upsert*);
drh46d2e5c2018-04-12 13:15:435678#else
drh2549e4c2020-12-08 14:29:035679#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
drh46d2e5c2018-04-12 13:15:435680#define sqlite3UpsertDelete(x,y)
drh2549e4c2020-12-08 14:29:035681#define sqlite3UpsertDup(x,y) ((Upsert*)0)
drh61e280a2020-12-11 01:17:065682#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
5683#define sqlite3UpsertNextIsIPK(x) 0
drh46d2e5c2018-04-12 13:15:435684#endif
5685
danielk1977d8293352009-04-30 09:10:375686
dan75cbd982009-09-21 16:06:035687/* Declarations for functions in fkey.c. All of these are replaced by
5688** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
5689** key functionality is available. If OMIT_TRIGGER is defined but
5690** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
mistachkinbfc9b3f2016-02-15 22:01:245691** this case foreign keys are parsed, but no other functionality is
dan75cbd982009-09-21 16:06:035692** provided (enforcement of FK constraints requires the triggers sub-system).
5693*/
5694#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan8ff2d952013-09-05 18:40:295695 void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
dand66c8302009-09-28 14:49:015696 void sqlite3FkDropTable(Parse*, SrcList *, Table*);
dan8ff2d952013-09-05 18:40:295697 void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
dane7a94d82009-10-01 16:09:045698 int sqlite3FkRequired(Parse*, Table*, int*, int);
5699 u32 sqlite3FkOldmask(Parse*, Table*);
dan432cc5b2009-09-26 17:51:485700 FKey *sqlite3FkReferences(Table *);
drh44a5c022022-01-02 12:01:035701 void sqlite3FkClearTriggerCache(sqlite3*,int);
dan1da40a32009-09-19 17:00:315702#else
dan8ff2d952013-09-05 18:40:295703 #define sqlite3FkActions(a,b,c,d,e,f)
dan67896ce2013-10-14 15:41:395704 #define sqlite3FkCheck(a,b,c,d,e,f)
dand66c8302009-09-28 14:49:015705 #define sqlite3FkDropTable(a,b,c)
dan67896ce2013-10-14 15:41:395706 #define sqlite3FkOldmask(a,b) 0
5707 #define sqlite3FkRequired(a,b,c,d) 0
dane7eeeb92017-01-30 11:38:195708 #define sqlite3FkReferences(a) 0
drh44a5c022022-01-02 12:01:035709 #define sqlite3FkClearTriggerCache(a,b)
dan1da40a32009-09-19 17:00:315710#endif
dan75cbd982009-09-21 16:06:035711#ifndef SQLITE_OMIT_FOREIGN_KEY
dan1feeaed2010-07-23 15:41:475712 void sqlite3FkDelete(sqlite3 *, Table*);
drh6c5b9152012-12-17 16:46:375713 int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
dan75cbd982009-09-21 16:06:035714#else
dan1feeaed2010-07-23 15:41:475715 #define sqlite3FkDelete(a,b)
drh6c5b9152012-12-17 16:46:375716 #define sqlite3FkLocateIndex(a,b,c,d,e)
dan75cbd982009-09-21 16:06:035717#endif
5718
drh643167f2008-01-22 21:30:535719
5720/*
5721** Available fault injectors. Should be numbered beginning with 0.
5722*/
5723#define SQLITE_FAULTINJECTOR_MALLOC 0
drh7e8b8482008-01-23 03:03:055724#define SQLITE_FAULTINJECTOR_COUNT 1
drh643167f2008-01-22 21:30:535725
5726/*
danielk1977ef05f2d2008-06-20 11:05:375727** The interface to the code in fault.c used for identifying "benign"
drhd12602a2016-12-07 15:49:025728** malloc failures. This is only present if SQLITE_UNTESTABLE
danielk1977ef05f2d2008-06-20 11:05:375729** is not defined.
drh643167f2008-01-22 21:30:535730*/
drhd12602a2016-12-07 15:49:025731#ifndef SQLITE_UNTESTABLE
danielk19772d1d86f2008-06-20 14:59:515732 void sqlite3BeginBenignMalloc(void);
5733 void sqlite3EndBenignMalloc(void);
drh643167f2008-01-22 21:30:535734#else
danielk19772d1d86f2008-06-20 14:59:515735 #define sqlite3BeginBenignMalloc()
danielk1977867d05a2008-06-23 14:03:455736 #define sqlite3EndBenignMalloc()
drh643167f2008-01-22 21:30:535737#endif
drh643167f2008-01-22 21:30:535738
drh3a856252014-08-01 14:46:575739/*
5740** Allowed return values from sqlite3FindInIndex()
5741*/
5742#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
5743#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
5744#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
5745#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
drhbb53ecb2014-08-02 21:03:335746#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
drh3a856252014-08-01 14:46:575747/*
5748** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
5749*/
drhbb53ecb2014-08-02 21:03:335750#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
5751#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
5752#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
drh2c041312018-12-24 02:34:495753int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
danielk19779a96b662007-11-29 17:05:185754
drhff6b8262016-03-04 00:13:295755int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
5756int sqlite3JournalSize(sqlite3_vfs *);
dand67a9772017-07-20 21:00:035757#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
5758 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
danielk1977f55b8992007-08-24 08:15:535759 int sqlite3JournalCreate(sqlite3_file *);
danielk1977c7b60172007-08-22 11:22:035760#endif
5761
dan2491de22016-02-27 20:14:555762int sqlite3JournalIsInMemory(sqlite3_file *p);
danielk1977b3175382008-10-17 18:51:525763void sqlite3MemJournalOpen(sqlite3_file *);
danielk1977b3175382008-10-17 18:51:525764
drh2308ed32015-02-09 16:09:345765void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
drh0224d262008-05-28 13:49:345766#if SQLITE_MAX_EXPR_DEPTH>0
drhb6dad522021-09-24 16:14:475767 int sqlite3SelectExprHeight(const Select *);
drh7d10d5a2008-08-20 16:35:105768 int sqlite3ExprCheckHeight(Parse*, int);
danielk1977fc976062007-05-10 10:46:565769#else
drh0224d262008-05-28 13:49:345770 #define sqlite3SelectExprHeight(x) 0
drh7d10d5a2008-08-20 16:35:105771 #define sqlite3ExprCheckHeight(x,y)
danielk1977fc976062007-05-10 10:46:565772#endif
drhe30ecbf2023-06-13 18:10:525773void sqlite3ExprSetErrorOffset(Expr*,int);
danielk1977fc976062007-05-10 10:46:565774
drha3152892007-05-05 11:48:525775u32 sqlite3Get4byte(const u8*);
drha3152892007-05-05 11:48:525776void sqlite3Put4byte(u8*, u32);
5777
danielk1977404ca072009-03-16 13:19:365778#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
5779 void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
5780 void sqlite3ConnectionUnlocked(sqlite3 *db);
5781 void sqlite3ConnectionClosed(sqlite3 *db);
5782#else
5783 #define sqlite3ConnectionBlocked(x,y)
5784 #define sqlite3ConnectionUnlocked(x)
5785 #define sqlite3ConnectionClosed(x)
5786#endif
5787
drh6e736832007-05-11 12:30:035788#ifdef SQLITE_DEBUG
5789 void sqlite3ParserTrace(FILE*, char *);
5790#endif
drh0d9de992017-12-26 18:04:235791#if defined(YYCOVERAGE)
5792 int sqlite3ParserCoverage(FILE*);
5793#endif
drh6e736832007-05-11 12:30:035794
drhb0603412007-02-28 04:47:265795/*
5796** If the SQLITE_ENABLE IOTRACE exists then the global variable
mlcreech3a00f902008-03-04 17:45:015797** sqlite3IoTrace is a pointer to a printf-like routine used to
mistachkinbfc9b3f2016-02-15 22:01:245798** print I/O tracing messages.
drhb0603412007-02-28 04:47:265799*/
5800#ifdef SQLITE_ENABLE_IOTRACE
mlcreech3a00f902008-03-04 17:45:015801# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
danielk1977b4622b62007-03-02 06:24:195802 void sqlite3VdbeIOTraceSql(Vdbe*);
mistachkin9871a932015-03-27 00:21:525803SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
drhb0603412007-02-28 04:47:265804#else
5805# define IOTRACE(A)
danielk1977b4622b62007-03-02 06:24:195806# define sqlite3VdbeIOTraceSql(X)
drhb0603412007-02-28 04:47:265807#endif
drhb0603412007-02-28 04:47:265808
drh107b56e2010-03-12 16:32:535809/*
5810** These routines are available for the mem2.c debugging memory allocator
5811** only. They are used to verify that different "types" of memory
5812** allocations are properly tracked by the system.
5813**
5814** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
5815** the MEMTYPE_* macros defined below. The type must be a bitmask with
5816** a single bit set.
5817**
5818** sqlite3MemdebugHasType() returns true if any of the bits in its second
5819** argument match the type set by the previous sqlite3MemdebugSetType().
5820** sqlite3MemdebugHasType() is intended for use inside assert() statements.
drh107b56e2010-03-12 16:32:535821**
drh174b9a12010-07-26 11:07:205822** sqlite3MemdebugNoType() returns true if none of the bits in its second
5823** argument match the type set by the previous sqlite3MemdebugSetType().
drh107b56e2010-03-12 16:32:535824**
5825** Perhaps the most important point is the difference between MEMTYPE_HEAP
drh174b9a12010-07-26 11:07:205826** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
5827** it might have been allocated by lookaside, except the allocation was
5828** too large or lookaside was already full. It is important to verify
5829** that allocations that might have been satisfied by lookaside are not
5830** passed back to non-lookaside free() routines. Asserts such as the
5831** example above are placed on the non-lookaside free() routines to verify
mistachkinbfc9b3f2016-02-15 22:01:245832** this constraint.
drh107b56e2010-03-12 16:32:535833**
5834** All of this is no-op for a production build. It only comes into
5835** play when the SQLITE_MEMDEBUG compile-time option is used.
5836*/
5837#ifdef SQLITE_MEMDEBUG
5838 void sqlite3MemdebugSetType(void*,u8);
mistachkin77978a62021-10-12 02:17:395839 int sqlite3MemdebugHasType(const void*,u8);
5840 int sqlite3MemdebugNoType(const void*,u8);
drh107b56e2010-03-12 16:32:535841#else
5842# define sqlite3MemdebugSetType(X,Y) /* no-op */
5843# define sqlite3MemdebugHasType(X,Y) 1
drh174b9a12010-07-26 11:07:205844# define sqlite3MemdebugNoType(X,Y) 1
danielk1977e3026632004-06-22 11:29:025845#endif
drh174b9a12010-07-26 11:07:205846#define MEMTYPE_HEAP 0x01 /* General heap allocations */
drhd231aa32014-10-07 15:46:545847#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
drhb2a0f752017-08-28 15:51:355848#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
drh107b56e2010-03-12 16:32:535849
drhf51446a2012-07-21 19:40:425850/*
5851** Threading interface
5852*/
drhbf20a352014-04-04 22:44:595853#if SQLITE_MAX_WORKER_THREADS>0
drhf51446a2012-07-21 19:40:425854int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
5855int sqlite3ThreadJoin(SQLiteThread*, void**);
drhbf20a352014-04-04 22:44:595856#endif
drhf51446a2012-07-21 19:40:425857
drha43c8c82017-10-11 13:48:115858#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
5859int sqlite3DbpageRegister(sqlite3*);
5860#endif
drh3e0327d2015-05-11 11:59:155861#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
5862int sqlite3DbstatRegister(sqlite3*);
5863#endif
drh107b56e2010-03-12 16:32:535864
drhb6dad522021-09-24 16:14:475865int sqlite3ExprVectorSize(const Expr *pExpr);
5866int sqlite3ExprIsVector(const Expr *pExpr);
drhfc7f27b2016-08-20 00:07:015867Expr *sqlite3VectorFieldSubexpr(Expr*, int);
drh10f08272021-07-05 01:11:265868Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
dan44c56042016-12-07 15:38:375869void sqlite3VectorErrorMsg(Parse*, Expr*);
dan71c57db2016-07-09 20:23:555870
drh89997982017-07-11 18:11:335871#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
danda1f49b2017-06-16 19:51:475872const char **sqlite3CompileOptions(int *pnOpt);
drh89997982017-07-11 18:11:335873#endif
danda1f49b2017-06-16 19:51:475874
drh20a9ed12022-09-17 18:29:495875#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
5876int sqlite3KvvfsInit(void);
5877#endif
5878
dan231ff4b2022-12-02 20:32:225879#if defined(VDBE_PROFILE) \
5880 || defined(SQLITE_PERFORMANCE_TRACE) \
5881 || defined(SQLITE_ENABLE_STMT_SCANSTATUS)
drh7741f342022-11-29 17:52:045882sqlite3_uint64 sqlite3Hwtime(void);
5883#endif
5884
dan45163fc2023-02-28 19:39:595885#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan06382de2023-02-28 20:04:015886# define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
dan45163fc2023-02-28 19:39:595887#else
5888# define IS_STMT_SCANSTATUS(db) 0
5889#endif
5890
drh43f58d62016-07-09 16:14:455891#endif /* SQLITEINT_H */